Jump to content

Getting id names


dumdumsareyum

Recommended Posts

Hi....I would like to be able to create some navigation links using javascript (can't use php cause the files won't necessary be on a server with php, just need to be openable in a browser).

I was thinking maybe there is an array to get all the id's listed in the document?  Or if there is an array for all the <h1> and <h2> elements that would be even better because I would like to include subtopics in my navigation tree.  My Javascript book lists getElementsById and getElementsByName and getElementsByTagName, but it doesn't explain any of them, it's a really crummy book. I didn't know if any of those would work for what I want to do because all I need is a list of what the IDs are, and then i guess i could do a loop on get element by id and display the text of the heading associated with that id.....  Any help would be much appreciated.

Link to comment
https://forums.phpfreaks.com/topic/99767-getting-id-names/
Share on other sites

here's the function I tried to do this, I thought it might help illustrate what I want to do:

 

function writeNavigation()
{
  var headings = document.getElementsByTagName("h1");
   for( var i=0; i< headings.length; i++) {
     var theID = headings[i].getAttribute("id");
     document.write("<a href='index.html#".theID.'>headings[i]</a><br />");
     }
}

Is this headed in the right direction?

Link to comment
https://forums.phpfreaks.com/topic/99767-getting-id-names/#findComment-510271
Share on other sites

nevermind, i figured it out, this is what i was going for:

 

function writeNavigation()
{
  document.write("I'm in the function <br />");
  var headings = document.getElementsByTagName('h1');
      for(i = 0; i < headings.length; i++)
       {
       var theID = headings[i].getAttribute("id");
       document.write('<a href="index.html#'+theID+'">'+headings[i].innerHTML+'</a><br />');
       }
}

Link to comment
https://forums.phpfreaks.com/topic/99767-getting-id-names/#findComment-510657
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.