John Smith John Smith - 1 month ago 6
jQuery Question

Behave differently when selecting a dropdown select by click or arrow keys

<select id="s">
<option value="1">selected1</option>
<option value="2">selected2</option>
<option value="3">selected3</option>
<option value="4">selected4</option>
</select>
<div id="se">
</div>

$('body').on('change', '#s', function() {
$('#se').text($('#s :selected').text()+" / unfortunatly it also triggers when Im moving with the UP and DOWN keys, not just with select");
});


https://jsfiddle.net/8kLke2je/

I want this event triggered if I pick an item from the dropbox with mouseclick and not with UP and DOWN arrows.

Answer

Just use the "click" event on the select:

    $('#s').click(function(){
            var sv = $('#s').val();
            $('#se').html("That\'s it: "+sv);
    });

Here there is a fiddle for it: https://jsfiddle.net/nemoneminis/rwza9xzt/1/

Comments