Domas
Domas

Reputation: 1133

Check if selected dropdown value is empty using jQuery

Here is the dropdown in question:

<select name="data" class="autotime" id="EventStartTimeMin">
    <option value=""></option>
    <option value="00">00</option>
    <option value="10">10</option>
    <option value="20">20</option>
    <option value="30">30</option>
    <option value="40">40</option>
    <option value="50">50</option>
</select>

What I want to do is check if the current value is empty:

if ($("EventStartTimeMin").val() === "") {
   // ...
}

But it does not work, even though the value is empty. Any help is much appreciated.

Upvotes: 29

Views: 144531

Answers (3)

Rajat_Kumar_India
Rajat_Kumar_India

Reputation: 99

You can try this also-

if( !$('#EventStartTimeMin').val() ) {
// do something
}

Upvotes: 9

Felix
Felix

Reputation: 38112

You need to use .change() event as well as using # to target element by id:

$('#EventStartTimeMin').change(function() {
    if($(this).val()===""){ 
        console.log('empty');    
    }
});

Fiddle Demo

Upvotes: 5

Rory McCrossan
Rory McCrossan

Reputation: 337713

You forgot the # on the id selector:

if ($("#EventStartTimeMin").val() === "") {
    // ...
}

Upvotes: 53

Related Questions