tags:

views:

55

answers:

3

i am trying to get the text of the selected drop down

I'm trying:

$(document).ready(function() {
    $("#product_category_id").change(function(){
        alert($(this).val())
    })
});

Also, how do I set option of a drop down to a specific option:

$(document).ready(function() {
    $("#product_category_id").change(function(){
      //set prodcut_prod_type drop down to option ""
        $("#product_prod_type").set
    })
});
A: 

The $.val() method is both for getting, and settting:

$("#product_prod_type").val( $("#product_category_id").val() );

If you wanted only the text of the selection option, you could do something like this:

$("#product_category_id").change(function(){
  var text = $("option:selected", this).text();
});
Jonathan Sampson
A: 

Very good, the first one is working. For the second question, I am not sure, do you want to preselect an option? If so:

$(document).ready(function() {
    $("#product_category_id option[value=value of option]").attr('selected', 'selected');
});
Felix Kling
+2  A: 

To get text (label, not value) of the selected option:

$(document).ready(function() {
    $("#product_category_id").change(function(){
        alert(this.options[this.selectedIndex].text);
    })
});
Qwerty