Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blur event on select boxes and Firefox

I have the following situation:

One selectbox and a tooltip that appears when the user clicks on the box to select an option. To show the tooltip can be easily done with css (select:focus ~ .tooltip) or jquery using the focus() event. When the user picks something the select box closes and the tooltip dissapears. This can be done with the change() event.

But there is one issue. If the user opens the selectbox and clicks somewhere else on the page, the list closes and in Firefox the blur event is not triggered right away, so the tooltip remains visible. If the user makes the second click outside of the select the blur event triggers and the tooltip dissapears.

Chrome and IE is ok, Firefox is not.

Do somebody know a workaround in Firefox?

thanks, Istvan

like image 704
Trombitás Avatar asked Jun 26 '13 16:06

Trombitás


People also ask

What triggers blur event?

The onblur event occurs when an object loses focus. The onblur event is most often used with form validation code (e.g. when the user leaves a form field). Tip: The onblur event is the opposite of the onfocus event. Tip: The onblur event is similar to the onfocusout event.

What is the difference between Blur and Focusout?

The main difference between this event and blur is that focusout bubbles while blur does not. The opposite of focusout is focusin .

How do you stop event blurs?

If you want to prevent the blur event from being fired, you have to do so when you are inside the mousedown event, you can do so by invoking the method preventDefault() on the event. Click the checkbox, focus input & then click the button, the textfield never loses focus now.

Does blur happen before click?

It looks like click event has lower priority than blur, so it is predictible behaviour that blur event fires first.


1 Answers

After playing around with this for about half an hour, I'm afraid to say my input would be: no. And for the following reasons:

  • Firefox doesn't fire the blur event until the second click. This is evident from looking at the dropdown on the select, which remains blue.
  • Therefore a pure CSS solution would definitely never work
  • A JavaScript solution would also be next to impossible too, as the first click seems to go nowhere
    • I've checked this by trying to note body and document clicks, you'll see that neither fire the first time. In fact, neither does the select, so I have on which level that click registers

See my JSFiddle for my workings. Sorry! I guess it's just a FF issue.

$(document).click(function() {
    console.log("document");
});
$("body").click(function() {
    console.log("body");
});
$("select").click(function(e) {
    e.stopPropagation();
    console.log("select");
});

Edit: Sorry, posted an old JSFiddle.

like image 111
Ian Clark Avatar answered Sep 22 '22 13:09

Ian Clark