|
How to set the value of a form field using JavascriptThis is in continuation of the article: set the value of a form element using Javascript.In this section, we will see how to set the value of other input fields like hidden feld, drop down list etc. Setting the value of a hidden field using JavascriptAs in the case of the textarea and text input elements, in order to set the value of a hidden input element in a form, we can use the following syntax:
oFormObject.elements["hidden_element_name"].value = 'Some Value';
Let us look at an example to illustrate how to set the value of a hidden input element through javascript. The form in this demo has a hidden element "login_attempts" that keeps a record of the number of times the login has been attempted. In order to provide this functionality, we need to first use a hidden input element in the HTML of the form:
<form name="login_frm" id="login_frm" action="#">
As you can see above, the onClick event handler
function checkLogin(loginElem, passwordElem, loginAttempts) {
The code is self-explanatory. When the form first loads, the login_attempts hidden element has an empty string as it's value. So, when the onClick event of the form's button element is triggered for the first time, the function above sets the value of the hidden element to 1. Thereafter, each time, this function is called, it increments the value of this element by 1. The code above also checks for the login and the password and prompts the user for invalid login attempts. More details can be looked up in the code sample for this example. Next: Set select options in a select, check box and more... Other Pages
|
|
. Copyright © 2003-2009 JavaScript-coder.com. All rights reserved. |
||