Greetings,
How to check if selected index of dropdownlist is not 0 or -1 using jquery?
Greetings,
How to check if selected index of dropdownlist is not 0 or -1 using jquery?
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) {
// ...
}
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.