jQuery in Action, 2nd edition*
The moose likes HTML, CSS and JavaScript and the fly likes Code without getElementById() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Code without getElementById()" Watch "Code without getElementById()" New topic
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: 2046
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: 29249
    
139

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: 60057
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by 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.
 
Don't get me started about those stupid light bulbs.
 
subject: Code without getElementById()
 
Similar Threads
Problems in acessing parent window from child
checkbox must always be checkd
How to remove Textbox Element using DOM in HTML
problem with jsp for reset() method
help w/ a background change script