Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery(document).on ('focus')?

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?

like image 592
user961627 Avatar asked Feb 18 '13 12:02

user961627


2 Answers

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

like image 78
Adil Avatar answered Oct 29 '22 19:10

Adil


Try:

$(document).on('focus click', 'input',  function(e){
        console.log("focused on " + e.target.id);
});

Also just e.target.id is enough..

like image 40
Anujith Avatar answered Oct 29 '22 19:10

Anujith