i have:
<input type="text" />
and
$('input').blur(function(){ alert('stay focused!'); });
I want to prevent the blur function running when I'm "blurring" by clicking on an anchor element.
I.E. if i tab to another input, click somewhere on the page etc i want the blur to fire, but if i click a link, I don't want it to fire.
Is this easily achievable, or do i need to hack about with delegates and semaphores?
Thanks
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!'); $('input').focus(); mousedownHappened = false; } else // blur event is okay { // Do stuff... } }); $('a').mousedown(function() { mousedownHappened = true; });
Hope this helps you!!
If you want to keep the cursor at its position in a contenteditable
element, simply:
$('button').mousedown(function(){return false;});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With