Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 616
  • Last Modified:

Javascript: Difference between getElementById and document.forms

What is the difference between document.getElementById() and document.forms[0].element
0
karakav
Asked:
karakav
1 Solution
 
slinkygnCommented:
GetElementById complies with the W3C Level 1 DOM, while the forms[] array does not -- it's Level 0 DOM.  In general, the first is recommended unless you need compatibility with very old browsers.

But beyond that, both will give you a DOM object in return, and so are functionally similar.
0
 
Tomarse111Commented:
Is it not just a different way of getting the same object?

document.forms is part of the DOM specification and so should not be unnecessarily avoided in favour of getElementById, simply use whatever is easiest.
0
 
karakavAuthor Commented:
Thanks.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now