Reputation:
How to listen to multiple events like change and input, but fire only one at time.
Example:
Using this code to listen to the events, but on input it's fire twice.
Demo:
https://jsfiddle.net/h6sny738/2/
$(document).on("change input", '.test', function(event) {
console.log("test");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<input class="test">
Upvotes: 1
Views: 82
Reputation: 36784
Name your function and attach different listeners with different targets:
var myFunc = function(event) {
console.log('test', event.type, event.target.tagName);
};
$(document)
.on('change', 'select.test', myFunc)
.on('input', 'input.test', myFunc);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<input class="test">
<select class="test">
<option>1</option>
<option>2</option>
</select>
Upvotes: 1