pavan kumar pavan kumar -4 years ago 72
HTML Question

Fire change() event even when selection hasn't changed

I want to override the general behavior of an HTML

select
element by making it fire the change event even when the selected option hasn't changed.



$('select').change(function() {
alert($(this).val());
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>





How can i achieve this?

Answer Source

You can try like this:-

HTML :

<select>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
</select> 

JS :

$("select").mouseup(function() {
    var flag = $(this).data("flag");
        var cur_val = $(this).val();
    if(flag) {
        alert(cur_val);
    }
    $(this).data("flag", !flag);
});

Here is working jsfiddle.

Give it a try, this should work.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download