0

Possible Duplicate:
How to get the selected value of dropdownlist using JavaScript?

I have a drop down list box, whose entries are dynamically populated from mysql using php.

Here is the snippet.

   <?php
        //include '../db/interface/DB_Manager.php';
        $connect = mysql_connect("localhost", "root", "infinit") or die(mysql_error()); 
        mysql_select_db("serverapp") or die(mysql_error()); 

        $query = "select id,name from rpt_shift_def"; //Write a query
        $data = mysql_query($query);  //Execute the query
    ?>
    <select id="selectShift" name="selectShift" onchange="ready(this.value)">
    <?php
    while($fetch_options = mysql_fetch_array($data)) { //Loop all the options retrieved from the query
    ?>

    <option id ="<?php echo $fetch_options['id']; ?>"  value="<?php echo $fetch_options['name']; ?>"><?php echo $fetch_options['name']; ?></option><!--Echo out options-->
    <?php
        }
    ?>
    </select>

Now i want the values in the option id's to pass it to the Mysql query to use those values as filters(those ID's are UUID of Mysql used here as Primary Keys).

If the id's are static i can use document.getElementById("ID"); to get those values, since it was PHP echo's, i can not use document.getElementById().

How to get the values in echo'ed in the ID in Javascript.

Community
  • 1
  • 1
Allen
  • 681
  • 5
  • 16
  • 30

1 Answers1

0

Thanks to Marwan Alsabbagh, The use of options[e.selectedIndex].value solves my Problem.

   select = document.getElementById("selectShift");
   select.onchange = function(){
        var options = this.getElementsByTagName("option");
        optionHTML = options[this.selectedIndex].value; 
   }
Allen
  • 681
  • 5
  • 16
  • 30
  • If all your options have a value, then all you need inside the listener is `optionHTML = this.value;`. There is certainly no need for `getElementsByTagName`, select elements have an options property that is a collection of all their options: `var options = this.options;`, but you don't need that anyway. – RobG Nov 09 '12 at 06:31