• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Code without getElementById()

 
Ivan Turner
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been looking at and rewriting this code from a textbook written in 2003. I believe the code is deprecated or at least old. I was under the impression the more up to date code uses getElementById(). Does anyone know how do get the same functionality using getElementById()?

BTW, I added the id attribute to most tags.
 
Jesus Angeles
Ranch Hand
Posts: 2068
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where did you get the idea that such code is deprecated or at least old? Just because it was written in 2003?

getElementByName can also be used.

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34396
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quite literally:


There are other ways to make the code better though. Passing the parameters isn't necessary. You could get those directly as well. Or you could use a library like jQuery to make the code shorter.

Note that getElementByName doesn't do the same thing as getElementsById. Names are not guaranteed to be unique on a page.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64832
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesus Angeles wrote:Where did you get the idea that such code is deprecated or at least old? Just because it was written in 2003?

It is old and decrepit. Using that form of addressing is antiquated and error-prone. Not as bad as stuff like document.forms[0] though. That's just a bug waiting to happen.
 
Ivan Turner
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Quite literally:


There are other ways to make the code better though. Passing the parameters isn't necessary. You could get those directly as well. Or you could use a library like jQuery to make the code shorter.

Note that getElementByName doesn't do the same thing as getElementsById. Names are not guaranteed to be unique on a page.


Thanks Jeanne. I haven't tried it yet but what you wrote looks good. The code I posted was using only the name attribute because, as you probably know, it's designed to work that way. Thanks for the pointers and advice.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic