Javascript that auto populates a form

Posted on 2007-08-05
Last Modified: 2013-11-05
I am creating a form and I was wondering how can I populate a form automatically by checking yes or no from another field without submitting the form?
Question by:thenone
    LVL 63

    Expert Comment

    You can do anything by script. Accessing fields by script and setting the values are the basic scripting operations.
    But you did not say where the data should come from when you said without submit. The data should be already on the page and wait for the click to populate the fields.
    Show an example of data and the names of the fields to populate and I can show you how to do it.
    LVL 2

    Accepted Solution

    By looks of it, you are wanting to do something like 'is your billing address the same as your shipping addres'

    if that is the case, you would do this...

    in your checkbox --> ONCLICK="setValues()"

    <SCRIPT LANGUAGE="JavaScript">
    function setValues(){
        if (document.getElementById('checkBox').checked == true){
       document.getElementById('inputValueToSet').value = document.getElementById('inputvaluetoget').value;

    put the script at the top of the page.  also change 'checkBox', 'inputValueToSet', and 'inputValueToGet' to the names of the inputs that you want to use.

    hope this helps and is what your looking for.
    LVL 8

    Author Comment

    ok its a questionaire with pints of value on it.


    do you like dogs      Y/N           10 Points   Value

    If they click y from the drop down menu then the value is automatically populated without refreshing the screen or clicking submit.
    LVL 63

    Assisted Solution

    Populated to what field? And what mean populated? Does the target field get exact this value: 10
    What are the other values beside Y and N of the dropdown? If the first value is Y how should the script know the target field should get 10 as value when no user action can change Y to Y?

    LVL 2

    Expert Comment

    Here is a quick page that I think does what you want...

    <SCRIPT LANGUAGE="JavaScript">
          function awardPoints(itm){
                ptsName = + "Pts";
                if(itm.value == 1){
                      document.getElementById(ptsName).value = 10;
                      document.getElementById(ptsName).value = "";

    <FORM NAME="Questionaire" ID="Questionaire">
          Your question here?
          <SELECT ID="Select1" NAME="Select1" ONCHANGE="awardPoints(this)">
                <OPTION VALUE="0">Select an Answer
                <OPTION VALUE="1">YES
                <OPTION VALUE="2">NO
          <INPUT TYPE="TEXT" VALUE="" NAME="Select1Pts" ID="Select1Pts" SIZE="3">

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Suggested Solutions

    In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at (http://www.experts-ex…
    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now