| Author |
Code without getElementById()
|
Ivan Turner
Ranch Hand
Joined: Feb 27, 2012
Posts: 37
|
|
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
Joined: Feb 26, 2005
Posts: 2036
|
|
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
internet detective
Marshal
Joined: May 26, 2003
Posts: 26183
|
|
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.
|
[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56185
|
|
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.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Ivan Turner
Ranch Hand
Joined: Feb 27, 2012
Posts: 37
|
|
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.
|
 |
 |
|
|
subject: Code without getElementById()
|
|
|