Results 1 to 5 of 5

Thread: How to set the value of a form element using Javascript?

  1. #1
    Join Date
    Aug 2006
    Posts
    122

    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.
    In the end, we will remember not the words of our enemies, but the silence of our friends.
    -Martin Luther King Jr.

  2. #2
    Join Date
    Jul 2006
    Posts
    442

    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.
    "When they give you ruled paper, write the other way..." J.R.J.

  3. #3
    Join Date
    Nov 2008
    Posts
    996

    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';

  4. #4
    Join Date
    Feb 2008
    Posts
    1,856

    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>

  5. #5
    Join Date
    Mar 2008
    Posts
    672

    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>

Similar Threads

  1. javascript get element by class
    By GlassFish in forum Software Development
    Replies: 5
    Last Post: 06-03-2010, 08:05 PM
  2. Php form with javascript
    By Solaris in forum Software Development
    Replies: 3
    Last Post: 30-11-2009, 01:22 PM
  3. Code to scroll to javascript element.
    By sizzla in forum Software Development
    Replies: 2
    Last Post: 04-07-2009, 10:13 PM
  4. JavaScript - How to create Element
    By Xylina in forum Software Development
    Replies: 3
    Last Post: 04-06-2009, 05:06 PM
  5. How do I know the width of an element through javascript?
    By Conner in forum Software Development
    Replies: 3
    Last Post: 06-04-2009, 11:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •