ratan
ratan

Reputation: 429

how to get text of the selected value in drop down

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
    })
});

Upvotes: 1

Views: 388

Answers (3)

Qwerty
Qwerty

Reputation: 1742

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

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

Upvotes: 2

Sampson
Sampson

Reputation: 268334

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();
});

Upvotes: 0

Felix Kling
Felix Kling

Reputation: 816312

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');
});

Upvotes: 0

Related Questions