- document.getElementById(‘frmMain’).elements
assumes the form has an ID and that the ID is unique as IDs should be. Although it also accesses aname
attribute in IE, please add ID to the element if you want to use getElementById
- document.getElementsByName(‘frmMain’)[0].elements
will get the elements of the first object named frmMain on the page – notice the plural getElements – it will return a collection.
- document.getElementsByTagName(‘form’)[0].elements
will get the elements of the first form on the page based on the tag – again notice the plural getElements
A great alternative is
- document.querySelector(“form”).elements
will get the elements of the first form on the page. The “form” is a valid CSS selector
- document.querySelectorAll(“form”)[0].elements
notice theAll
– it is a collection. The [0] will get the elements of the first form on the page. The “form” is a valid CSS selector
In all of the above, the .elements
can be replaced by for example .querySelectorAll("[type=text]")
to get all text elements