how to prevent blur() running when clicking a link in jQuery?

I had to solve this problem myself today, too. I found that the mousedown event fires before the blur event, so all you need to do is set a variable that indicates that a mousedown event occurred first, and then manage your blur event appropriately if so.

var mousedownHappened = false;

$('input').blur(function() {
    if(mousedownHappened) // cancel the blur event
        alert('stay focused!');
        mousedownHappened = false;
    else   // blur event is okay
        // Do stuff...

$('a').mousedown(function() {
    mousedownHappened = true;

Hope this helps you!!

Leave a Comment