|
How to get the value of a form element using JavaScriptPlease refer article: how to get JavaScript form object for information on getting a reference to the form object. In this article we demonstrate the use of Javascript for accessing the values of form elements. Later, we will demonstrate all the concepts using a real world example. Getting a text input element and it's valueTo obtain a reference to a text input element, we can use a construct like this:
oText = oForm.elements["text_element_name"]; OR
In the code above, "index" is the position of the element in
the 0-based
oForm = document.forms[index];
To get the value of the text input element, we can use the
text_val = oText.value;
As an example, if we have the following text input element:
<input type="text" name="name" id="txt_name" size="30" maxlength="70">
We can access the value of the element like this:
name = oForm.elements["name"].value;
Getting a textarea element and it's valueThe syntax for obtaining a reference to a textarea element is very similar:
oTextarea = oForm.elements["textarea_element_name"];
To get the value entered by the user in the textarea field:
textarea_val = oTextarea.value;
As an example, if we have a textarea element like this:
<textarea name="address" id="txta_address" rows="3" cols="35"></textarea>
We can access the value entered by the user in this way:
address = oForm.elements["address"].value;
Getting a hidden element and it's valueThe syntax for obtaining a reference to a hidden input element:
oHidden = oForm.elements["hidden_element_name"];
To get the value of this element, we use the familiar code construct:
hidden_val = oHidden.value;
As an example, if we have a hidden input element in the form defined like this:
<input type="hidden" name="number_of_skillsets" value="1">
We can get the hidden input element's value like this:
number_of_skillsets = oForm.elements["number_of_skillsets"].value;
Getting a select-one element and the selected optionAs before, we can obtain a reference to this type of element using its name:
oSelectOne = oForm.elements["select_one_element_name"];
To get the index of the selected option in the javascript
index = oSelectOne.selectedIndex;
We can now use this index to determine the value of the selected option:
var selected_option_value = oSelectOne.options[index].value;
If we needed to get the text corresponding to the selected option, we would
need to use the
var selected_option_text = oSelectOne.options[index].text;
Let us say we need to find out the country selected by the user from a select-one list:
<select size="1" id="slt_country" name="country">
The Javascript code that can be used to get the user-selected country:
var selected_index = oForm.elements["country"].selectedIndex;
Getting a select-multiple element and all the selected optionsIf a select element has the additional attribute 'multiple', it allows users to select more than one option. Have a look at this HTML fragment:
<select name="job_category" id='slt_job_category' size="4"
multiple='multiple'>
We can write a Javascript function to get all the user-selected options:
function getSelectedOptions(oList) {
To use this function, we must first obtain a reference to the select-multiple object:
oList = oForm.elements["job_category"];
We now pass this object reference to the getSelectedOptions function above. In this function, we iterate over all the options in the Getting a radio element and it's checked valueRadio buttons in a form can be grouped together using the same name attribute, such that, if one of them is checked, all the others are automatically un-checked. Let us look at an example:
<input type="radio" name="work_abroad" value="y">Yes
In order to retrieve the value of the checked radio button, we can write a simple javascript function:
function getRadioCheckedValue(radio_name) {
The code is self-explanatory. We obtain a reference to the group of radio buttons using the name of the group, and then iterate over all the radio button objects to test which one of them is checked. Getting a checkbox element, it's value and determining whether it is checked or notA checkbox in a form has only two states (checked or un-checked) and is independent of the state of other checkboxes in the form, unlike radio buttons which can be grouped together under a common name. Let us look at the HTML code showing two checkboxes:
<input type="checkbox" name="email_alerts" id="chk_email_alerts" value="ON" checked>Email me matching jobs everyday
In order to access these checkboxes, their values, and their states, we can use the following javascript function:
function testCheckbox(oCheckbox) {
We can then use this function in the following way:
oCheckBox1 = oForm.elements["email_alerts"];
ExampleIn order to demonstrate the concepts, let us look at an example form. Please fill out the form completely, so that we can discuss certain features. Some observations:
The rest of the functionality is quite typical and has already been explained in this article, and you can download the code to learn more.
Related: |
|
. Copyright © 2003-2009 JavaScript-coder.com. All rights reserved. |
||