TL;DR how can I get this self-explanatory JSFiddle to work?
From the W3C:
The blur event occurs when an element loses focus either via the pointing device or by tabbing navigation. 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");
});
It doesn't work. A more illustrative example is in this JSFiddle.
I also tried focusout
, with this JSFiddle, but (presumably because it bubbles up from the input), it always fires.
I could probably rig up what I need with a hack like this: https://stackoverflow.com/a/5049387/458614 but I'd rather not have to.
Edit: There are lots of related questions and I have read all that I could find, none of which help. Some talk about setting tabindex=0
on the form
or label
elements. I have tried this in various permutations but it doesn't help. JSFiddle here. If you put it on the form
, blur
events do fire when you click outside the form. However, it doesn't apply to any of it's children: it won't pick up anything if you click on the input
and then outside the form
.
Edit 2: I don't really understand some of the answers posted so far and none seem to really... work. Anyway, to clarify, here is what I am trying to accomplish:
In my app, you can add tags to documents. When you click the "add tag" button, a previously-hidden text input field pops up and is focused. And then...
The problem is that #1 and #3 are incompatible. The "add tag" button needs to perform a different action based on whether the text field is open or closed, but because I can only achieve #1 with an onblur
event on the text field, the text field is closed by the time any action happens on the "add tag" button for #3.
Here is a JSFiddle with my best attempt so far.
The blur event occurs when an element loses focus either via the pointing device or by tabbing navigation. This event is valid for the following elements: LABEL, INPUT, SELECT, TEXTAREA, and BUTTON. It doesn't work. A more illustrative example is in this JSFiddle.
The opposite of blur is focus. There are two ways of implementing event delegation for this event: by using the focusout event, or by setting the useCapture parameter of addEventListener () to true. See implementation notes.
If the element that has the onBlur effect and tabindex is created onClick of another element, it does not automatically gets focus when it appears. Thus, you may need to focus it using element.focus () after creating the element. You will have to set a tabindex on the div and manually focus it to get the blur handler invoked
onblur Event 1 Definition and Usage. The onblur event occurs when an object loses focus. The onblur event is most often used with form validation code (e.g. ... 2 Browser Support 3 Syntax. Note: The addEventListener () method is not supported in Internet Explorer 8 and earlier versions. 4 Technical Details 5 More Examples
The thing I think you are looking for is
e.stopPropagation();
This Fiddle here shows a little different way to handle it ... it put the hide on a window click (which would blur the input anyways) except on the label, which it would allow the click event to stop inside the label.
Happy coding!
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