Solved

Updating multiple input fields using one select box.

Posted on 2004-04-21
4
987 Views
Last Modified: 2009-12-16
I'm trying to automatically populate three input fields by using one select (combo box) field.  The select field contains names, when a name is selected, their home, cell, and work phone numbers need to be populated into the three input fields.

Does anyone have any ideas about how to do this?

Here's the code for the select and input fields:

<select size="1" name="em_name1" class="input_shadowless">
<option>Select</option>
<option>User 1</option>
<option>User 2</option>
<option>User 3</option>
</select>
<input class="input_shadowless" size=19 maxlength="50" type="text" name="em_work_phone1">
<input class="input_shadowless" size=19 maxlength="50" type="text" name="em_cell_phone1">
<input class="input_shadowless" size=19 maxlength="50" type="text" name="em_home_phone1">

My knowledge of JavaScript is extremely small, so spelling it out plainly would greatly help!!

Thanks,
Steve
0
Comment
Question by:scross1276
  • 2
4 Comments
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 50 total points
ID: 10878734
<script>

function user(p1,p2,p3) {
  this.p1=p1;
  this.p2=p2;
  this.p3=p3;
}

users = new Array(
new user('','',''),
new user('123123','31123123','234234'),
new user('223123','41123123','634234'),
new user('323123','51123123','734234')
)
function fill(sel) {
  frm = sel.form; // get the form the sel is in
  frm.em_work_phone1.value = users[sel.selectedIndex].p1
  frm.em_cell_phone1.value= users[sel.selectedIndex].p2
  frm.em_home_phone1.value= users[sel.selectedIndex].p3
}
</script>
<form>

<select size="1" name="em_name1" class="input_shadowless"
onCHange="fill(this)">
<option>Select</option>
<option>User 1</option>
<option>User 2</option>
<option>User 3</option>
</select>
<input class="input_shadowless" size=19 maxlength="50" type="text" name="em_work_phone1">
<input class="input_shadowless" size=19 maxlength="50" type="text" name="em_cell_phone1">
<input class="input_shadowless" size=19 maxlength="50" type="text" name="em_home_phone1">
</form>
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 10878775
So the "user" function is used to create a user object that has 3 attributes, p1, p2, p3 (could be changed in the script to workphone cellphone, homephone
We then create a users array (list) of 4 times a user
the first has empty values to clear the fields when the 0th option (Select) is chosen

The keyword (this) is used to pass the actual select to the function fill

Michel
0
 

Author Comment

by:scross1276
ID: 10878809
What can I say mplungjan, you're awesome!
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 10878876
Like this:


<html>
<head>
<script>
var nameTab = [
["", "", ""],
["work+U1", "cell+U1", "home+U1"],
["work+U2", "cell+U2", "home+U2"],
["work+U3", "cell+U3", "home+U3"]];
function setFields(theSel){
  theUser = nameTab[theSel.selectedIndex];
  theForm = theSel.form;
  theForm.em_work_phone1.value = theUser[0];
  theForm.em_cell_phone1.value = theUser[1];
  theForm.em_home_phone1.value = theUser[2];
}
</script>
</head>
<body>
<form>
<select size="1" name="em_name1" class="input_shadowless" onChange="setFields(this)">
<option>Select</option>
<option>User 1</option>
<option>User 2</option>
<option>User 3</option>
</select>
<input class="input_shadowless" size=19 maxlength="50" type="text" name="em_work_phone1">
<input class="input_shadowless" size=19 maxlength="50" type="text" name="em_cell_phone1">
<input class="input_shadowless" size=19 maxlength="50" type="text" name="em_home_phone1">
</form>
</body>
</html>


0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how can i get 4 nd 5 ? 3 31
Asp in script 6 39
Add or delete table rows 10 55
Html input text color the frame line 2 22
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…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

828 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