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

x
?
Solved

Javascript that auto populates a form

Posted on 2007-08-05
5
Medium Priority
?
2,020 Views
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?
0
Comment
Question by:thenone
  • 2
  • 2
5 Comments
 
LVL 63

Expert Comment

by:Zvonko
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.
0
 
LVL 2

Accepted Solution

by:
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()"

<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
 
LVL 8

Author Comment

by:thenone
ID: 19635635
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
 
LVL 63

Assisted Solution

by:Zvonko
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?


0
 
LVL 2

Expert Comment

by:UnTwstr
ID: 19639412
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

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