...

View Full Version : onChange select box?



nikko50
01-19-2006, 12:44 AM
Hi Guys:)
How can I change a select list when a user enters a value into a text box? Like with the below form I would like to have the select box change when the user enters a value in the "id" text box that matches a value in the select box. Say the user types "1122" in the text box I would like the select list to change to to show "John". Is this possible?? Please help. Tracy
<form method="POST" >

<p><input type="text" name="id" size="20"></p>
<p><select size="1" name="name">
<option value="1122">John</option>
<option value="1133">Fred</option>
<option value="1144">Joe</option>
<option selected>Select One</option>
&nbsp;
</select></p>

<p>
<input type="submit" value="Submit" name="B1"></p>
</form>

glenngv
01-19-2006, 02:24 AM
HTML:

<form>
<p><input type="text" name="id" size="20" onkeyup="selectOption(this)" onblur="selectOption(this)"></p>
<p><select size="1" name="selName">
<option value="1122">John</option>
<option value="1133">Fred</option>
<option value="1144">Joe</option>
<option value="" selected>Select One</option>
</select></p>
</form>
Javascript:

function selectOption(oTxt){
var sel = oTxt.form.selName;
var len = sel.options.length;
sel.selectedIndex = len - 1; //select 'Select One' option by default
for (var i=0; i<len; i++){
if (sel.options[i].value == oTxt.value){
sel.selectedIndex = i;
break;
}
}
}

nikko50
01-19-2006, 04:07 AM
You're the best.The script you sent works great but here's what I'm actually trying to do. I need to compare the first six charactors in the text field string. If the first 6 charactors are equal to "1ZR401" then the select list should change to value "01".If the first 6 charactors are equal to "1ZR402" then the select list should change to value "03".
If the first 6 charactors are equal to "1ZR403" then the select list should change to value "02". Could you help me with that?
Tracy

<form>
<p><input type="text" name="id" size="20" ></p>
<p><select size="1" name="selName">
<option value="01">UPS GROUND</option>
<option value="02">UPS NEXT DAY</option>
<option value="03">UPS SECOND DAY</option>
<option value="" selected>Select One</option>
</select></p>
</form>

glenngv
01-19-2006, 09:57 AM
Why not just use 1ZR401 to 03 as the option values? Do you need those 01-03 values on the server?

nikko50
01-19-2006, 12:34 PM
Yes. The 01-03 are the carrier id's located in a mysql table. Then I attach the full tracking number the user enters. UPS tracking numbers always start with "1Z9R29A401" or "1Z9R29A402" or "1Z9R29A403" depending on the service level. So actually I need to look at the first 10 charactors. Please help.
Tracy

glenngv
01-20-2006, 03:08 AM
<form>
<p><input type="text" name="id" size="20" onkeyup="selectOption(this)"></p>
<p><select size="1" name="selName">
<option value="01" id="1Z9R29A401">UPS GROUND</option>
<option value="02" id="1Z9R29A402">UPS NEXT DAY</option>
<option value="03" id="1Z9R29A403">UPS SECOND DAY</option>
<option value="" selected>Select One</option>
</select></p>
</form>
...
function selectOption(oTxt){
var sel = oTxt.form.selName;
var len = sel.options.length;
sel.selectedIndex = len - 1; //select 'Select One' option by default
for (var i=0; i<len; i++){
if (oTxt.value.indexOf(sel.options[i].id) == 0){
sel.selectedIndex = i;
break;
}
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum