Using JavaScript to access form objects when there are multiple forms
In order to access a form through Javascript, we need to obtain a reference to
the form object. One obvious way to approach, which you have already seen, is to use the
getElementById method. For instance, if we had a form with the id attribute
"subscribe_frm", we could access the form in this way:
var oForm = document.getElementById('subscribe_frm');
However, this approach has the limitation that the <form>
tag must have an id attribute, as the getElementById method
needs the id of an element in order to locate it in the DOM tree. Fortunately,
Javascript provides us with alternatives that can work for forms without an id
attribute present.
One way to approach is to make use of the this keyword which always points
to the object that is calling a particular method. Used in conjunction with
the onClick event handler of a form's submit button, we can get a reference to
the form itself, through the this.form property, even if there are multiple forms
on the page. We will see an example of this approach shortly. The other alternative is to use the document object's forms collection to obtain a reference to the form object. The forms collection is a Javascript array that holds references to all the forms present in a web page. We can access each form reference either by it's index in this array or by it's name attribute. Let us look at each of these methods of accessing multiple forms in details.
Accessing multiple Forms through Javascript using the this.form property
Every element in a form triggers events based on user action which can be handled by an appropriate event handler. For instance, the submit button on being clicked triggers the onClick event handler. Since the this keyword in Javascript always points to the object that calls a given method, the onClick event handler can be passed this self reference to obtain a reference to the button element object.
Moreover, in Javascript, every element object of a form has a property that references the form object. So, if we were to pass this.form to the onClick event handler of a form button, this function would get the reference to the form object. This is an easy way to access form objects, even if there are multiple forms on a web page, because the form's name or id attribute are never directly used. Let us have a look at an example.
<form name ="submit_bookmark" action="#">
Page Title: <input type="text" name="page_title" size="50">
Page URL: </label><input type="text" name="page_url" size="50">
Tags (comma separated): <input type="text" name="tags_list" size="40">
<input type="button" name="submit" value="Save Bookmark" onclick="showElements(this.form);">
</form>
In the form above, the showElements onClick event handler of the form button is being passed this.form. Here this refers to the button object, and this.form references the form object. We could now use this form object in the showElements function and access the form and all it's elements.
Have a look at this demo which shows two form on a web page. You can also download the code to learn more.
Accessing multiple Forms using the document object's forms collection
The document object of a web page has a property named forms which is a javascript array that holds referencs to all the form objects on the page. We can get a reference to individual forms by using the index of the forms or by using their names. Please note that this array's index begins at 0.
For instance, if we have two forms "submit_article" and "submit_bookmark" appearing on a web page in this order, we can access the form named "submit_bookmark" in the following two ways:
var oForm = document.forms[1]; OR
var oForm = document.forms["submit_bookmark"];
We could also use the shorthand notation: document.formname:
var oForm = document.submit_bookmark;
Please note, however, that the shorthand notation is deprecated, and is either not supported in recent versions of Firefox or is buggy and unreliable. It is also preferable to use the form's name attribute instead of the index, as it is less likely for the name of the forms on the page to change, and more likely for their sequential position in the code to change.
We can use the same approach to access individual form elements as well. Every form field of a form is contained in the form's elements collection which is a javascript array similar to the document object's forms collection.
To access the text input field named "tags_list" of the form named "submit_bookmark" (which was the second form on the web page, as you saw in the demo), we can use one of the following methods:
- First, get the form reference:
var oForm = document.forms["submit_bookmark"];
- Next, we get the reference to the "tags_list" element in one of the following ways:
var oTagsList = oForm.elements[2]; OR
var oTagsList = oForm.elements["tags_list"];
- To get the value of the input element, as before:
var tags = oTagsList.value;
Please have a look at this detailed demo to understand the concepts presented. You can also download the code to learn more.
As a further example, let us say that we had to copy the value of an element in one form into an element in another form. Have a look at this demo to understand the requirements
We could implement this by passing the names of the two forms as well as the names of the source and target elements to a javascript function:
<a href="#" onClick=" javascript: copyFormElementToElementOfDifferentForm('submit_article', 'submit_bookmark', 'site_cat', 'tags_list');">Copy site_cat element value of the first form to the input field named "tags_list" of the second form</a>
Have a look at the implementation of this function:
var oForm1 = document.forms[oForm1Name];
var oForm2 = document.forms[oForm2Name];
var oForm1Element = oForm1[oForm1ElementName];
var oForm2Element = oForm2[oForm2ElementName];
if(oForm2Element.value == '') {
oForm2Element.value += oForm1Element.value;
} else {
oForm2Element.value += ', ' + oForm1Element.value;
}
The code is self-explanatory. We first acquire references to form objects using their names. We then acquire references to the source and target elements, again using the element names. Thereafter, it is an easy job to copy the value of the source element to the target element.
You can download the code for this example, to learn more.
Related:
|