2

if I'm passing a jquery object that I know is a select object, how can I get the text (not the value) of the option that is selected?

I'm needing something like this.

...function ($select){

    var selectedText = $select.selected().text();

}

And since $select is already a jquery object, I cant really change the selector of the object to use ":selected".

2
  • I believe .text() should work. Post your code on JSFIDDLE, please. Commented Jul 22, 2013 at 18:36
  • You can query within the object to find children elements. Commented Jul 22, 2013 at 18:44

2 Answers 2

4
$select.find(':selected').text();

should do.

2

You can use this:-

Suppose you have a dropdown like this:-

    <select id="dropdown">
        <option value="aa">a</option>
        <option value="bb">b</option>
        <option value="cc">c</option>
    </select>

then javascript will be like this:-

   $(document).ready(function() {
        obj = $('#dropdown :selected')
        value = obj.val()
        alert(value) # will alert aa/bb/cc 

        text = obj.text()
        alert(text) # will alert a/b/c
})

Not the answer you're looking for? Browse other questions tagged or ask your own question.