Jeff Hu
Jeff Hu

Reputation: 744

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.

Upvotes: 0

Views: 303

Answers (2)

Zakaria Acharki
Zakaria Acharki

Reputation: 67505

You should create a function then pass it as parameter :

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

function blurred(){
   alert('blurred');
}

Hope this helps.

Upvotes: 2

Pointy
Pointy

Reputation: 413682

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.

Upvotes: 4

Related Questions