Back to Home

Form properties and methods

Forms and control elements, such as have a lot of special properties and events. Working with forms will be much more convenient when we learn them.

Navigation: form and elements

Document forms are members of the special collection document.forms. That’s a so-called “named collection”: it’s both named and ordered. We can use both the name or the number in the document to get the form. When we have a form, then any element is available in the named collection form.elements. For instance: There may be multiple elements with the same name. This is typical with radio buttons and checkboxes. In that case, form.elements[name] is a collection. For instance: These navigation properties do not depend on the tag structure. All control elements, no matter how deep they are in the form, are available in form.elements.

info

Backreference: element.form

For any element, the form is available as element.form. So a form references all elements, and elements reference the form. Here’s the picture: For instance:

Form elements

Let’s talk about form controls.

input and textarea

We can access their value as input.value (string) or input.checked (boolean) for checkboxes and radio buttons. Like this:

select and option

A : 1. Find the corresponding

new Option

option.selected

Is the option selected. option.index

The number of the option among the others in its , one can also get the value by the index select.selectedIndex or through the options collection select.options. These are the basics to start working with forms. We’ll meet many examples further in the tutorial. In the next chapter we’ll cover focus and blur events that may occur on any element, but are mostly handled on forms.

document.forms.my; // the form with name="my"
document.forms[0]; // the first form in the document
Example:

Follow the lesson from Microsoft Web-Dev-For-Beginners course

Tags: web,development