Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Javascript that auto populates a form

Posted on 2007-08-05
Medium Priority
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
  • 2
  • 2
LVL 63

Expert Comment

ID: 19635313
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.

Accepted Solution

UnTwstr earned 1800 total points
ID: 19635553
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()"

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.

Author Comment

ID: 19635635
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

Zvonko earned 200 total points
ID: 19637207
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?


Expert Comment

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

      function awardPoints(itm){
            ptsName = itm.name + "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">

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

580 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