...

View Full Version : auto fill text fields based on a dropdown selection



Twelvethman
12-14-2004, 10:14 PM
Have searched high and low for Javascript that would allow me to auto-populate five or so text fields in a form with corresponding values based on selection made at the top of the form in a dropdown option box.

Any directions would be welcome!

Bobo
12-14-2004, 10:43 PM
function Y(W)
{
switch(W)
{
case 1:this.value='that';otherdude.value='eggplant';break;
case 2:this.value='spatula';otherdude.value='gypsy';break;
case 3:this.value='mud';otherdude.value='pie';break;
default :alert('ERROR!');break;


}
}
...
<select name="T" onChange="Y(this.options[this.options.selectedIndex].value)">
<option value="1">A</option>

<option value="2">B</option>

<option value="3">C</option>
<option value="4">D</option>
</select>

glenngv
12-15-2004, 01:53 AM
function fillFields(oSel)
{
var oForm = oSel.form;
switch (oSel.options[oSel.selectedIndex].value)
{
case '1':oForm.field1.value='blah1';oForm.field2.value='foo1';break;
case '2':oForm.field1.value='blah2';oForm.field2.value='foo2';break;
case '3':oForm.field1.value='blah3';oForm.field2.value='foo3';break;
}
}
...
<form>
<select name="T" onchange="fillFields(this)">
<option value="1">item 1</option>
<option value="2">item 2</option>
<option value="3">item 3</option>
</select>
<input type="text" name="field1" />
<input type="text" name="field2" />
</form>

Twelvethman
12-15-2004, 03:23 PM
Will give that a shot later today and send a response.

Twelvethman
12-16-2004, 11:06 PM
:thumbsup: :thumbsup:

thank you very much - the code works verrrry nicely!!

xo_tiff_xo
12-17-2004, 05:50 PM
How would you use this code when trying to populate the values dynamically from a database?

glenngv
12-18-2004, 06:21 AM
http://www.codingforums.com/showthread.php?p=258167#post258167



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum