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?
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?
2 Answers
Reset to default 7You 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..