tags:

views:

68

answers:

3

Greetings,

How to check if selected index of dropdownlist is not 0 or -1 using jquery?

+2  A: 

Unless you've the actual option element to your hands, jQuery doesn't have special facilities for this. Just access the element's standard selectedIndex attribute:

var selectedIndex = $('#dropdownId').attr('selectedIndex');
if (selectedIndex > 0) {
    // ...
}
BalusC
Umm don't you mean `$('#dropdownId').get(0).selectedIndex` or `$('#dropdownId').attr('selectedIndex')` ?
Pointy
Or `$('#dropdownId')[0]` :)
Nick Craver
@Pointy: indeed.
BalusC
+2  A: 
$("select#elem").get(0).selectedIndex > 0
Glennular
@pointy I don't see any difference from what i posted to what you commented to BalusC
Glennular
Sorry; it looks like you updated it.
Pointy
+1  A: 

Taking a different approach from the others, jQuery can check the "value" of the option, as well.

<select id="checkme">
    <option value="0">0</option>
    <option value="-1">-1</option>
</select>

And the jQuery:

$(document).ready( function () {
    var theValue = $("#checkme").val();
    alert("The value of the select is: " + theValue);

});

This way you don't have to know what index maps to what value, you just check the value of the select, and it will tell you what option is selected.

S Pangborn