How to set the value of a form element using Javascript?
Hi. I am new to this forum. Hope that I am posting at right place.!! I have recently started with the JavaScript. Before that I did C, C++ and little bit of Java. Since I am new I don't know much about the JavaScript. I want your help for setting the value of a form element using Javascript? Please help me as soon as possible..!! Since, I am new to this forum, please ignore my mistakes.
Re: How to set the value of a form element using Javascript?
Before moving to the actual coding, since you are new, I would like you to suggest to know more about the form in JavaScript. The first and best hidden is the form property. Also you should know that the form property of an element is not declared by you, but the system generates for you. In simple words you can say that it is simply a pointer that points back to the form containing the element. In JavaScript, you need to refer to the form from within the elements object. The 'name' and 'value' are the two most important properties that you declare as attributes of the form elements. The name stores the name of the form field. And the value property contains the value you want to pass back from that form field.
Re: How to set the value of a form element using Javascript?
Setting the value of a form element is very important for the form. Its the first step of it. For setting the value of a textarea element using Javascript, you should read the described instructions. You can use the following syntax for setting the value :
Code:
oFormObject.elements["element_name"].value = 'Value Demo';
Suppose that you are accessing the form object through any of the form's elements, then you can use the following syntax :
Code:
this.form.elements["element_name"].value = 'Value Demo';
Re: How to set the value of a form element using Javascript?
I am given very simple sample of the code, so that you can understand exactly why the value of a form element are set and how. Just have a look at the following coding :
Code:
<form name="admission_form" action="#">
Full Name: <input type="text" size="30" maxlength="60" name="name" >
Address: <input type="text" size="30" maxlength="350" name="email" >
Admission: <textarea name="admission_form" rows="45" cols="130"></textarea>
<input type="button" name="submit" value="Submit Admission" onclick="showElements(this.form);" >
</form>
Re: How to set the value of a form element using Javascript?
You will also have to use the oFormObject.elements for setting the value of a text input field element in a form. For using that, the following is the syntax :
Code:
oFormObject.elements["element_name"].value = 'Some Value';
For more proper understanding about the setting the value of a text input field element in a form, just go through the coding :
HTML Code:
<form name="register_complaint" action="#">
Full Name: <input type="text" size="30" maxlength="60" name="name" >
Address: <input type="text" size="30" maxlength="250" name="address" >
Admission: <textarea name="admission_form" rows="45" cols="130"></textarea>
Months as member: <input type="text" size="5" maxlength="5" name="membership_period" >
<input type="button" name="increase" value="+" onclick="monthModify(this.form.elements["membership_period"], 'incr');" >
<input type="button" name="decrease" value="-" onclick="monthModify(this.form.elements["membership_period"], 'decr');" >
<input type="button" name="submit" value="Submit Complaint" onclick="showElements(this.form);" >
</form>