Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jan 2004
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts

    setting selectedIndex on a drop down to the index of a perticular value?

    How would I get the index number of a select list for a known value?

    Example:
    Code:
    <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.

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    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>
    Last edited by Mr J; 08-13-2004 at 08:06 PM.

  • #3
    Regular Coder
    Join Date
    Jun 2004
    Posts
    565
    Thanks
    0
    Thanked 18 Times in 18 Posts
    Code:
    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
    Last edited by dumpfi; 08-13-2004 at 08:11 PM.

  • #4
    Regular Coder
    Join Date
    Jan 2004
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Mr J
    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.
    Code:
    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.
    Code:
    <select name="inputDriverID" onchange="getNonCavalier();">
    	<cfoutput query="qGetDriverInfo"><option value="#driverID#"<cfif inputDriverID eq #driverID#> selected</cfif>>#driverName#</cfoutput>
    </select>

  • #5
    Regular Coder
    Join Date
    Jan 2004
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts
    dumpfi, thanks! Got it working with your help!
    Code:
    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;
    			}
    		}
    	}
    }
    Last edited by FastCougar; 08-13-2004 at 08:31 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •