Jeff Hu Jeff Hu - 5 months ago 14
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
alert('blurred')
.

More interesting is, the
focus()
works well.

Thanks for any suggestion or help.

Answer

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.