For some strange reason this simple function doesn’t seem to work:
$("body").on("focus", this, function(){
alert('d');
})
this is input or textarea element.
If i were to do this:
$(this).on("focus", function(){
alert('d');
})
It would work on those elements that exists at the moment, but i event would not fire up on newly created elements, what am i doing wrong?
The second parameter is, as described by the API, “a selector string to filter the descendants of the selected elements that trigger the event.”
I assume
this, in your case, is a DOM element. Try changing that to a selector to match inputs or textareas. This will cause your function to be called whenever afocusevent bubbles up tobodyfrom an element matching your selector. This should work for you:Further information on
on()here: http://api.jquery.com/on/