Jump to content


getelementbyid troublesy


6 replies to this topic

#1 influct

    Young Padawan

  • Members
  • Pip
  • 63 posts

Posted 03 December 2007 - 06:56 PM

Im sure this is something really basic, but I'm tierd:p

I have a set up like this:

<div id='hi'>

<span id='hi2'>

</span>

</div>

i need to access the span, but after dictating the div first (not tag name specific) this is as i have many "spans" with the same id but within "divs". I tried something like this:

var 1 = document.getelementbyid('hi');

var 2 = var1.getelementbyid('hi2');

it didnt take:p

thanks!

#2 dotbart

    Young Padawan

  • Members
  • Pip
  • 141 posts
  • Gender:Male
  • Location:Diepenbeek
  • Interests:Webdesign, Webdeveloppement, DJ, ...

Posted 03 December 2007 - 07:04 PM

var 1 = document.getElementById('hi');

javascript is case sensitive ;-)

#3 influct

    Young Padawan

  • Members
  • Pip
  • 63 posts

Posted 03 December 2007 - 07:10 PM

ah sorry, should have specified my code was a bit rough, the real thing has the correct case:)

thanks anyway.

#4 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 03 December 2007 - 07:13 PM

Quote

i have many "spans" with the same id
That's a BIG no-no. The point of an ID is to identify something. If everybody were walking around with the same ID's, how would we be able to legally tell who's who? Same point applies here (except in a slightly more strict case).

If you want everything to have a similar attribute in common, make it a class, not an id.

Since i also believe getElementById is document specific, you could run a loop through the div till you run into something with a matching id... but of course, i remember jerry-rigging something like this myself (using your method) and it worked fine.

#5 influct

    Young Padawan

  • Members
  • Pip
  • 63 posts

Posted 03 December 2007 - 07:25 PM

i understand that and thanks, but is there not a way to search inside a predefined area for the matching id? in this case i want to search for the id hi2 within the area hi1, not the entire document. although i trust your opinion so will assume as you said this is not feasible. would you mind posting some sample code as for workarounds?

Edit: no wait thats something else im thinking of:(

thanks!

Edited by influct, 03 December 2007 - 07:58 PM.


#6 influct

    Young Padawan

  • Members
  • Pip
  • 63 posts

Posted 03 December 2007 - 08:01 PM

I have resolved to simply do it by name. why i did not do this before i can't begin to imagine, thanks for correcting me over my misconceptions and setting me on the right path!

#7 Wildhoney

    Young Padawan

  • Members
  • Pip
  • 31 posts

Posted 05 December 2007 - 07:38 PM

Even W3C will pull you on your duplicate IDs just for future reference :) !





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users