Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blur event stops click event

Example code: http://jsfiddle.net/slolife/PnmxM/

I am asking this even though there are a number of similar questions, but I feel that they are not the same.

I have an textbox that when it is blurred, should do something.

I also have a link, that is always visible (that appears to be how the other questions differ) and when clicked, should do something.

My blur handler fires, but the click handler does not.

Is there a proper way to deal with this?

Update

Many people pointed out that the alerts were causing my problem. Thank you. In my real code, I do not have alerts, but instead remove the textbox from the DOM.

So I have updated the fiddle to better reflect that with console.log calls instead of alert() calls. Any additional help is appreciated.

like image 679
slolife Avatar asked Feb 27 '12 23:02

slolife


People also ask

What is the difference between Blur and Focusout?

The focusout event fires when an element has lost focus, after the blur event. The two events differ in that focusout bubbles, while blur does not. The opposite of focusout is the focusin event, which fires when the element has received focus.

What is a blur () in HTML?

The HTMLElement. blur() method removes keyboard focus from the current element.

Can we use Blur event on div?

The blur event fires when focus is lost. By default, a div element cannot have the focus in the first place so it can't be lost. If you set tabindex on a div, then it can gain the focus, but you should almost always be using a more appropriate element (such as a button) when you think about making interactive controls.


3 Answers

This is because of the first alert breaking the second alert because it's modal. See this:

  • http://jsfiddle.net/PnmxM/4/

Here, I'm appending the message to msgs div and it works as expected.

For your updated jsFiddle, here's an (updated-updated?) working one:

  • http://jsfiddle.net/PnmxM/10/

You are removing the input box in your onBlur and, as a consequence of that, moving the Add item vertically, thus the click doesn't happen on Add item anymore (as your mouse pointer did not move in the meantime), but on some other element (in this case, a jsFiddle example container). Moving Add item above the disappearing input element solves the "click me if you can" issue.

like image 92
icyrock.com Avatar answered Sep 21 '22 19:09

icyrock.com


The click event is not firing because that only happens when you release the mouse button. That isn't happening because there is a modal dialog window visible (the alert window). If you change the alerts to console.log instead then it works perfectly...

http://jsfiddle.net/PnmxM/7/

like image 31
Reinstate Monica Cellio Avatar answered Sep 21 '22 19:09

Reinstate Monica Cellio


Because you are using an alert you are interrupting the execution, which causes a problem for me in Firefox - but if you switch to console.log and make sure you have a console (like Firebug) open, you can see both events fire.

function onBlur() {
    console.log('blur');
}

function addItem() {
    console.log('add');
}

$(document).ready(function() {
    $('#items').delegate('input','blur', onBlur);
    $('#addItem').click(addItem);
});
like image 41
Fenton Avatar answered Sep 25 '22 19:09

Fenton