Javascript that auto populates a form

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?
LVL 8
thenoneAsked:
Who is Participating?
 
UnTwstrCommented:
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;
    }
}
</SCRIPT>

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.
0
 
ZvonkoSystems architectCommented:
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.
0
 
thenoneAuthor Commented:
ok its a questionaire with pints of value on it.

Example

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.
0
 
ZvonkoSystems architectCommented:
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?


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

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

<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
      </SELECT>
      <INPUT TYPE="TEXT" VALUE="" NAME="Select1Pts" ID="Select1Pts" SIZE="3">
</FORM>
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.