***
form.myTextBox.value document.getElementById("myTextBox"); Brief clarification building on 's question and 's reply: referencing a control through a form is a form-scoped lookup that finds controls by name (and sometimes id) within that form, while the document-level lookup returns an element by its id across the whole page. That difference matters when there are multiple forms with controls that share the same name, or when controls share names with DOM properties.
The form-scoped collection and its named access are explained in the HTMLFormElement docs; it exposes controls via the form's elements collection and will return a single control or a collection (for same-name groups like radio buttons) depending on the match (HTMLFormElement.elements). The document method looks up an id across the document and returns one element or null (Document.getElementById).
Practical examples:
var f = document.forms['orderForm']; // get the specific form
var qty = f.elements['quantity']; // form-scoped lookup by name
console.log(qty.value); var el = document.querySelector('#unique-field'); // explicit id-based selector
console.log(el.value); Notes and best practices: prefer explicit selectors for clarity—use form.elements or form-scoped lookups when the code is operating inside a particular form (avoids collisions when multiple forms use the same name). Use id-based selectors or querySelector when a control is meant to be unique. Avoid naming controls with identifiers that shadow built-in form/window properties (for example names like "submit" or "action"), and prefer predictable, unique ids for scripts that must target a single element. For selector and collection behavior details see the MDN links above (HTMLFormElement.elements, Document.getElementById).
Jump to Post— Tulsa 1hi
as per my view if in your page have a more than one form tag and have a same textbox name that time this is better way to useform.myTextBox.value;rather than this way.
b'cos it give you directly value from the current document
hi
as per my view if in your page have a more than one form tag and have a same textbox name that time this is better way to use
form.myTextBox.value; rather than this way.
b'cos it give you directly value from the current document
document.getElementById("myTextBox").value; Thanks
Thank you. This clearly explains when to use what...
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.