TL; DR How can I get this self-evident JSFiddle to work?
From W3C :
A blur event occurs when an element loses focus either through a pointing device or by navigating through a tab. This event is valid for the following elements: LABEL, INPUT, SELECT, TEXTAREA and BUTTON.
The basic idea, HTML:
<form> <label> <input type="text" /> <a href="#">after focusing in input, there should be no blur when clicking here</a> </label> </form> <a href="#">but blur should fire when clicking here</a>
And JS:
$("form, label").on("blur", function() { alert("you're not going to see this"); });
This does not work. A more obvious example is this JSFiddle .
I also tried focusout , this JSFiddle , but (apparently because it is bubbling from the input), it always fires.
Perhaps I could fine-tune what I need with a hack: https://stackoverflow.com/a/166269/2127/ , but I would not want that.
Edit: There are many related questions, and I read everything I could find, none of which would help. Some talk about setting tabindex=0 form or label elements. I tried this in different permutations, but it does not help. JSFiddle here . If you put it on a form , blur events fire when you step outside the form. However, this does not apply to any of these children: it will not pick anything up if you click on input and then outside the form .
Edit 2: I really don't understand some of the answers posted so far, and none of them seem really ... work. In any case, to clarify, here is what I am trying to accomplish:
In my application, you can add tags to documents. When you click the "Add Tag" button, a pop-up and previously focused text input field appears. And then...
- Clicking outside (on blur) should close the input text box again
- When you press the enter key, add the tag and close the input field
- Clicking the Add Tag button should also add the tag and close the input field.
The problem is that # 1 and # 3 are incompatible. The add tag button should perform another action based on whether the text field is open or closed, but since I can only reach # 1 with the onblur event in the text field, the text field closes by the time any action occurs when the " Add tag to # 3.
Here is a JSFiddle with my best attempt so far .