How to set the value of a form element using Javascript

8:47:00 PM 0 Comments

The syntax for accessing the form object is as below:

oFormObject = document.forms['myform_id'];

For accessing individual elements, we use the following code:

oformElement = oFormObject.elements[index];

OR

oFormElement = oFormObject.elements["element_name"];

In the above code, “index” refers to the position of the element in the “elements” collection array, and “element_name” is the name of the element. Both approaches give us a reference to the desired form element.

Setting the value of a textarea element using JavaScript

In order to set the value of a textarea field element in a form, we can use the following code:

oFormObject.elements["element_name"].value = 'Some Value';

If we are accessing the form object through any of the form’s elements itself, we can also use the following code:

this.form.elements["element_name"].value = 'Some Value';

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard.