Java Reference
In-Depth Information
All of the HTML elements used for interaction should be placed inside an HTML form. Let's start
by taking a look at HTML forms and how you interact with them in JavaScript.
html forms
Forms provide you with a way of grouping together HTML interaction elements with a common
purpose. For example, a form may contain elements that enable the input of a user's data for
registering on a website. Another form may contain elements that enable the user to ask for a car
insurance quote. It's possible to have a number of separate forms in a single page. You don't need
to worry about pages containing multiple forms until you have to submit information to a web
server—then you need to be aware that the information from only one of the forms on a page can be
submitted to the server at one time.
To create a form, use the <form> and </form> tags to declare where it starts and where it ends.
The <form/> element has a number of attributes, such as the action attribute, which determines
where the form is submitted; the method attribute, which determines how the information is
submitted; and the target attribute, which determines the frame to which the response to the
form is loaded.
Generally speaking, for client‐side scripting where you have no intention of submitting information
to a server, these attributes are not necessary. For now the only attribute you need to set in the
<form/> element is the name attribute, so that you can reference the form.
So, to create a blank form, the tags required would look something like this:
<form name="myForm">
</form>
You won't be surprised to hear that these tags create an HtmlFormElement object, which you can
use to access the form. You can access this object in two ways.
First, you can access the object directly using its name—in this case document.myForm .
Alternatively, you can access the object through the document object's forms collection property.
Remember that Chapter 8 included a discussion of the document object's images collection and
how you can manipulate it like any other array. The same applies to the forms collection, except
that instead of each element in the collection holding an HtmlImageElement object, it now holds an
HtmlFormElement (hereby called simply Form ) object. For example, if it's the first form in the page,
you reference it using document.forms[0] .
Note Of course, you can also access a form using the document
.getElementById() and document.querySelector() methods.
Many of the attributes of the <form/> element can be accessed as properties of the
HtmlFormElement object. In particular, the name property mirrors the name attribute of the <form/>
element.
 
Search WWH ::




Custom Search