Coding with JavaScript For Dummies
Book image
Explore Book Buy On Amazon

In JavaScript, the properties of the Form object match up with the attributes of the HTML form element. They’re used for getting or setting the values of the HTML form element attributes with JavaScript. Check out the list of all the properties of the Form object.

DOM objects are representations of HTML pages. Their purpose is to give you access (also known as programming interface) to the different parts of the document through JavaScript. Anything within an HTML document can be accessed and changed with JavaScript by using the DOM.

Property Use
acceptCharset Gets or sets a list of character sets that are supported by the server.
action Gets or sets the value of the action attribute of the form element.
autocomplete Gets or sets whether input elements can have their values automatically completed by the browser.
encoding Tells the browser how to encode the form data (either as text or as a file). This property is synonymous with enctype.
enctype Tells the browser how to encode the form data (either as text or as a file).
length Gets the number of controls in the form.
method Gets or sets the HTTP method the browser uses to submit the form.
name Gets or sets the name of the form.
noValidate Indicates that the form does not need to be validated upon submittal.
target Indicates the place to display the results of a submitted form.

After referencing the form using one of these methods, you then access the property using dot notation or the square bracket method.

To get the value of the name property of the first form in a document, you could use the following statement:

document.getElementByTagName("form")[0].name

A more common way to access a form is by assigning it an id attribute and using getElementById to select it.

The DOM provides another, more convenient method for accessing forms: the forms collection. The forms collection lets you access the forms in a document in two different ways:

  • By index number: When a form element is created in the document, it is assigned an index number, starting with zero. To access the first form in the document, use document.forms[0].

  • By name: You can also access forms using the value of the name attribute of the form element. For example, to get the value of the action property of a form with a name of subscribeForm, you would use document.forms.subscribeForm.action. Or you can use the square brackets method of accessing properties and write document.forms[subscribeForm].action.

About This Article

This article is from the book:

About the book authors:

Chris Minnick is an accomplished author, trainer, and web developer who has worked on web and mobile projects for both small and major businesses. Eva Holland is an experienced writer and trainer who has designed and taught online, in-person, and video courses. They are cofounders of WatzThis?

This article can be found in the category: