erosebe
erosebe

Reputation: 967

jQuery: How can I detect a blur event on all inputs

I've been trying things like

$(':input').blur(doStuff());

and

$('*').bind('blur', doStuff());

but nothing seems to trigger correctly. What's the best way to do this?

Upvotes: 1

Views: 2984

Answers (1)

adeneo
adeneo

Reputation: 318222

First of all, blur doesn't bubble, but jQuery sorta fixes that and makes it bubble, but you could still try focusout instead as that is an event that actually does bubble.

Secondly, you're calling the callback function, not referencing it, it should be

$('*').on('focusout', doStuff);

Thirdly, this is a really bad idea, listening for an event that bubbles on all elements in the DOM, you should probably at least just listen on the top most level

$(document).on('focusout', doStuff);

and even that is not really a very good idea, it would be better to target the elements directly.

Upvotes: 6

Related Questions