Is there a way to have keyup
, keypress
, blur
, and change
events call the same function in one line or do I have to do the
Is there a way to have
keyup
,keypress
,blur
, andchange
events call the same function in one line?
It's possible using .on()
, which accepts the following structure: .on( events [, selector ] [, data ], handler )
, so you can pass multiple events to this method. In your case it should look like this:
$('#target').on('keyup keypress blur change', function(e) {
// "e" is an event, you can detect the type of event using "e.type"
});
And here is the live example:
$('#target').on('keyup keypress blur change', function(e) {
console.log(`"${e.type.toUpperCase()}" event happened`)
});