Javascript: Difference between getElementById and document.forms

Posted on 2008-10-27
Last Modified: 2013-11-11
What is the difference between document.getElementById() and document.forms[0].element
Question by:karakav
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

slinkygn earned 500 total points
ID: 22810886
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.
LVL 15

Expert Comment

ID: 22810899
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.

Author Closing Comment

ID: 31510225

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…
The goal of the tutorial is to teach the user what frame rate is, how to control it and what effect it has on the video.

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question