Sasha
Sasha

Reputation: 8705

Jquery get text value of option on change

I have code like this:

<select name="caffe" id="caffe">
     <option value="5">Red</option>
     <option value="6">nmfxnsdnx</option>
</select>
<div id="gll_select">
     <script>
         var caffe = $("#caffe").val();
         var cff_name = $("#caffe option:first").text();
         $("#gll_select").load("http://localhost/010/form/galleries", {id : caffe}, function(){                 
        $("#gll").on('change', function(){
        var cff_name  = $("#caffe").text();
        get_gallery(cff_name);
        console.log(cff_name);
}); 
});  
     </script>
</div>

How can I get text value of the option that has been selected?

Upvotes: 1

Views: 7586

Answers (2)

mas-designs
mas-designs

Reputation: 7546

$('#caffe option:selected').text();

would do it for you ;-)

Upvotes: 1

James Allardice
James Allardice

Reputation: 166031

Change this line:

var cff_name  = $("#caffe").text();

To this:

var cff_name = $("#caffe option:selected").text();

This uses the :selected selector to find the option that is selected, and then gets the text of that, rather than getting the text of the entire select element.

Upvotes: 9

Related Questions