Solved

Updating multiple input fields using one select box.

Posted on 2004-04-21
4
984 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

708 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

13 Experts available now in Live!

Get 1:1 Help Now