[jquery] 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.

This question is related to jquery

The answer is


You forgot the # on the id selector:

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

You can try this also-

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

Try this it will work --

if($('#EventStartTimeMin').val() === " ") {

    alert("Please enter start time!");

}

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