Jishnu A P
Jishnu A P

Reputation: 14382

How to know if the textbox has lost focus due to a button click?

I have a textbox for which i have written the blur event handler and i have another button for which i have the click event written. i dont want to dispatch the blur event if it's due a click on the button.

Is there any way of doing this?

Upvotes: 2

Views: 1800

Answers (3)

Prakash
Prakash

Reputation: 6602

Try this :

function blurr() {
     alert('blur')   
}
$(function(){
    $('textarea').blur(function(){blurr()})
    $(':button').click(function(){alert('click')})
    $(':button').hover(function(){
        $('textarea').unbind('blur');
    },function(){
        $('textarea').bind('blur',function() {blurr()});
    })
})

Eg: jsFiddle

Upvotes: 1

Vivek
Vivek

Reputation: 11028

try focusout

Upvotes: 0

user657496
user657496

Reputation:

$('yourblurbutton').bind('blur', function() {
    yourblurfunction();
});
$('yourclickbutton').bind('click', function() {
    //your click code
});
$('yourclickbutton').bind('mouseover', function() {
    $('yourblurbutton').unbind('blur');
});
$('yourclickbutton').bind('mouseout', function() {
    $('yourblurbutton').bind('blur', function() {
        yourblurfunction();
    });
});
yourblurfunction() {
   // your blur function
}

The mouseover event will be triggered before the blur, so it makes us able to unbind the blur. On mouseout, we simple add the blur again.

Upvotes: 4

Related Questions