• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Code without getElementById()

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic