Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check a dropdown has a value in jquery?

Tags:

jquery

what is the best way to check is a drop down contains a value which isnt null.

 <select class="dropinput" name="Finish1" id="dropFinish1" tabindex="10" disabled="disabled">
    <option value=""></option>
</select> 

How to check the above drop down contains no values?

like image 501
Beginner Avatar asked Jan 31 '12 12:01

Beginner


1 Answers

if($('.dropinput > option[value!=""]').length == 0) {
    //dropdown contains no non-null options
}

This will check if the select box contains zero options that have a value different than "" (empty).

So the following HTML will be seen as empty in the above jQuery:

<select class="dropinput" name="Finish1" id="dropFinish1" tabindex="10" disabled="disabled">
    <option value=""></option>
</select> 

The following HTML will not be seen as empty:

<select class="dropinput" name="Finish1" id="dropFinish1" tabindex="10" disabled="disabled">
    <option value=""></option>
    <option value="some value"></option>
</select> 

JSFiddle demo

like image 126
Anders Arpi Avatar answered Nov 26 '22 02:11

Anders Arpi