|
Using JavaScript to access form objects when there are multiple formsIn 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
var oForm = document.getElementById('subscribe_frm');
However, this approach has the limitation that the One way to approach is to make use of the Accessing multiple Forms through Javascript using the this.form propertyEvery 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 Moreover, in Javascript, every element object of a form has a property that references the form object. So, if we were to pass
<form name ="submit_bookmark" action="#">
In the form above, the 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 collectionThe document object of a web page has a property named 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
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 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:
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];
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: |
|
. Copyright © 2003-2009 JavaScript-coder.com. All rights reserved. |
||