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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.