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

Automatically Create a Page Index (See related posts)

// description of your code here

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

<div id='indexDiv'>
   <div id='indexContents'></div>
</div>


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


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.

You need to create an account or log in to post comments to this site.


Click here to browse all 5140 code snippets

Related Posts