|
How to use getElementById to get the elements in a form
Sample code There are many ways of accessing form elements, of which the easiest is by using the cross-browser W3C DOM document.getElementById method. Before we learn more about this method, it would be useful to know something about the Document Object Model (DOM), the concept of HTML nodes or elements, and the concept of containers. Each time you load an HTML page, the web browser generates an internal representation of the page in the form of an inverted tree structure. Let us look at a simple form. We will use this form later to demonstrate the use of the getElementById method.
<form name ="subscribe" id="subscribe_frm" action="#">
In the above HTML form, each tag (such as
<input type="text" name="name" id="txt_name">
Here, the Accessing Form Elements using getElementByIdIn order to access the
var name_element = document.getElementById('txt_name');
The variable object name_element holds a reference to the input field in the
above form called name with the id attribute txt_name. Please note that
this method can only be used to access elements with a unique id attribute. Through
this object we can access all of the properties of the To access the value of the above input field entered by a user, we use the following syntax:
var name = name_element.value;
In a similar way, it is possible to access the
var frm_element = document.getElementById ('subscribe_frm');
Let us look at an example implementation of this method for accessing a form and all the user input elements of the form. As you can see from the demo, it is possible to toggle the visibility of the form itself.
This is done by accessing the form element
var frm_element = document.getElementById('subscribe_frm');
If we put this code in a function each time the function is called, it would toggle the visibility of the form. Each of the three user input elements: As an example, if you wanted to ensure that the name
function trim (str) {
We could then use this function:
var name_element = document.getElementById ('txt_name');
Checking for getElementById supportWe can check for the availability of the
if (document.getElementById) {
Please note that we only check for the method. We do not pass the id of the actual element that we need to access. For instance, the code below would be invalid:
if (document.getElementById('txt_name') {
The programs that we have seen so far work flawlessly in modern browsers that support the W3C DOM implementation. All recent versions of Mozilla Firefox, Opera, Internet Explorer and Netscape fall under this category. But if we had to support older browsers like Netscape 4 and Internet Explorer 4, we would have to use a different method to access form elements. For Netscape 4, we would have to check for the availability of the
We could write a function that manages all this for us:
function getElement (id) {
Other Cross-browser ways of accessing form elementsThere are two other cross-browser ways of accessing form elements:
Using the document.getElementsByTagName MethodThis method takes in as argument the name of a tag (element) and returns an array of all matching tags (elements) found in the document. In our form example, if we had to get a reference to all the
var inputs = document.getElementsByTagName('input');
The variable inputs is a reference to an array of all What if we only wanted to access
var inputs = document.getElementsByTagName("input");
This time, the elements retrieved do not include the element: Using the document.getElementsByName MethodThis method takes in as argument the name attribute value of elements to be retrieved and returns a collection of desired matching elements. In the code snippet below, let us say we need to get a reference to the
<select name="mail_format" id="slt_mail_format">
We could access the desired element in this way:
var mail_format_elements = document.getElementsByName('mail_format');
The
Related: |
|
. Copyright © 2003-2009 JavaScript-coder.com. All rights reserved. |
||