Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

getElementById  RSS feed

 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If my html has nested structure like



Assuming there is only ONE elemet whose id is called "myinput" (no ambiguity), does

document.getElementById("myinput") work ?

or should we do

document.getElementById("div1").getElementById("div2").getElementById("myinput")


I found sometimes it works using the first one but sometimes it doesn't.

please help explain. thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ben oliver wrote:
Assuming there is only ONE elemet whose id is called "myinput" (no ambiguity),

In any document all id values need to be unique. If they are not, the document is invalid and all bets are off.

does document.getElementById("myinput") work ?

Yes. For valid documents.

or should we do

Never.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!