Annabelle
Annabelle

Reputation: 754

jquery dropdown list detect if default value selected

How can I detect in jquery if default value of dropdown list is selected? The below code is triggered only if I change the previously selected value. If I do not change the value (I reselect the same value) the code is not triggered.

$('select[name="spinner-name"]').change(function(){ ... });

Upvotes: 0

Views: 106

Answers (1)

Konstantin T.
Konstantin T.

Reputation: 72

There is no change-event fired if there was no actual change in selection, but you can mimic the behavior from checking the current value from click-handler (as you are clicking on the dropdown when you "select" the previous value again).

    $("select").on({        
        "click": function() {
            alert('selected: '+$(this).val());
        },
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select>
<option>1</option>
<option>2</option>
<option>3</option>
</select>

Upvotes: 1

Related Questions