Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Style change only affects first element with matching id

Status
Not open for further replies.

AndyGroom

Programmer
May 23, 2001
972
GB
I've got this line of code and I was expecting it to hide all elements on my webpage with an id of 'nav', however it only hides the first element and ignores the others. How do I get it to apply the style to all 'nav' elements?
Code:
  document.getElementById('nav').style.display = 'none';

- Andy
___________________________________________________________________
If you think nobody cares you're alive, try missing a couple of mortgage payments
 
element ID's should be unique
you should not have duplicate ID's in any page
(try recoding to use classes instead.)
 
As IPGuru states ID's must be unique. as such the GetElementById function will only ever return a single object. The first element it finds with that ID. This is done because as ID's are meant to be unique there's no expectation of finding more than one. Which is why the function returns only one.

If you want to change multiple elements at once you'll need to use getElement[red]s[/red]By[blue]Name[/blue] and then manipulate the array it returns of all the elements that have that particular name.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thanks for the tips. I found this useful bit of code which solved the problem:

- Andy
___________________________________________________________________
If you think nobody cares you're alive, try missing a couple of mortgage payments
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top