...

View Full Version : setting selectedIndex on a drop down to the index of a perticular value?



FastCougar
08-13-2004, 08:37 PM
How would I get the index number of a select list for a known value?

Example:

<select name="inputTruck">
<option value="1">2644892
<option value="2">2594876
<option value="0">Non Company Truck
....
</select>

I need to find the index of 'inputTruck' where the value = 0. My list is output dynamically from a database that will change, but the value will not. I am sorting output of the dynamic option fields on their output text. As I add/delete trucks from the database, 'Non Company Truck' will move up the list, changing it's index with the selection.

Mr J
08-13-2004, 09:03 PM
Please try these example


<select name="inputTruck" onchange="alert(options.selectedIndex)">
<option value="1">2644892
<option value="2">2594876
<option value="0">Non Company Truck
....
</select>


<script>
function test(n){
alert(n)
}
</script>
<select name="inputTruck" onchange="test(this.options.selectedIndex)">
<option value="1">2644892
<option value="2">2594876
<option value="0">Non Company Truck
</select>

dumpfi
08-13-2004, 09:07 PM
function get_index() {
var f = document.forms['formName']['inputTruck'];
for(var i = 0; i < f.length; i++) {
if(f.options[i].value == '0') {
alert(i);
break;
}
}
}
window.onload = get_index;

Replace formName with the name of the form.

dumpfi

FastCougar
08-13-2004, 09:11 PM
Please try these example


<select name="inputTruck" onchange="alert(options.selectedIndex)">
<option value="1">2644892
<option value="2">2594876
<option value="0">Non Company Truck
....
</select>


<script>
function test(n){
alert(n)
}
</script>
<select name="inputTruck" onchange="test(this.options.selectedIndex)">
<option value="1">2644892
<option value="2">2594876
<option value="0">Non Company Truck
</select>I wish it where that simple ... I have another selection drop down that is a list of my drivers. I know the values of my "contracted" owner/operator drivers and have the following script "auto select" the truck drop down. Currently, the value is 8, but what happens when I stop leasing that truck and add 2 more due to growth. My current "8" will then be 9.

function getNonCompany(){
if(document.frmAddManifest['inputDriverID'].value == 1 || document.frmAddManifest['inputDriverID'].value == 2){
document.frmAddManifest['inputTruckID'].selectedIndex = 8;
}
}
This is the driver drop down, which calls the above function to auto change the next drop down for truck selection.
<select name="inputDriverID" onchange="getNonCavalier();">
<cfoutput query="qGetDriverInfo"><option value="#driverID#"<cfif inputDriverID eq #driverID#> selected</cfif>>#driverName#</cfoutput>
</select>

FastCougar
08-13-2004, 09:17 PM
dumpfi, thanks! Got it working with your help!
function getNonCavalier(){
if(document.frmAddManifest['inputDriverID'].value == 1 || document.frmAddManifest['inputDriverID'].value == 2){
var f = document.frmAddManifest['inputTruckID'];
for(var i = 0; i < f.length; i++) {
if(f.options[i].value == '0') {
document.frmAddManifest['inputTruckID'].selectedIndex = i;
break;
}
}
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum