Whenever the user focuses on clicks on an input, I want to trigger a method. I have the following code:
jQuery(document).on('focus click', function(e){
console.log("focused on " + $(e.target).attr('id'));
});
But whenever I focus on an element it just gives focused on undefined
. What's wrong with this code?
You missed the input selector in bind event using on().
jQuery(document).on('focus click', 'input', function(e){
console.log("focused on " + $(e.target).attr('id'));
});
syntax of on() .on( events [, selector ] [, data ], handler(eventObject) ), reference
Try:
$(document).on('focus click', 'input', function(e){
console.log("focused on " + e.target.id);
});
Also just e.target.id
is enough..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With