Jeff Hu Jeff Hu - 1 year ago 47
jQuery Question

Blur() doesn't work for this situation?

Here is my code:

<input id="m" autocomplete="off"/>

and I put the following lines in script

$('#m').on('blur', alert('blurred'););
$('#m').on('focus', alert('focused'););

The result is, it will pop up 'blurred' when the page starts. But afterward, it no longer fires the

More interesting is, the
works well.

Thanks for any suggestion or help.

Answer Source

The .on() method requires that you pass it a function. You're passing the return value of a call to alert().

$('#m').on('blur', function() { alert('blurred'); });

That code creates a simple anonymous function that makes your alert() call.