Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Patrick http://www.hunlock.com

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Automatically Create a Page Index

// description of your code here

First create your HTML. You can style it however you want but the division names must remain the same.

   1  
   2  <div id='indexDiv'>
   3     <div id='indexContents'></div>
   4  </div>


Next place the following code at the end of your content.
   1  
   2  els=document.getElementsByTagName('h4');
   3  idx='';
   4  for (i=0; i<els.length; i++) {
   5    idx += '<a href="#quickIDX'+i+'">'+els[i].innerHTML+'</a><BR>';
   6    els[i].innerHTML='<a name="quickIDX'+i+'"></a>'+els[i].innerHTML;
   7  }
   8  if (!els.length) {
   9    document.getElementById('indexDiv').style.display='none';
  10  } else {
  11    document.getElementById('indexContents').innerHTML=idx;
  12  }


Any H4 tag on the page will now be indexed and a link to the element placed in the index the script built. Place the index anywhere on your page and style it however you wish.
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS