Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onblur event not working with google chrome

I am trying to use onblur event to close a window. Its working fine in IE, Firefox and safari, but the event does not trigger in chrome. is that a bug in chrome or a mistake from me

like image 827
kiran Avatar asked Mar 09 '10 08:03

kiran


People also ask

Does tab trigger Onblur?

In browsers, pressing the tab key when a control has focus will move it to the next element in the tab order, causing the current element to lose focus and dispatch a blur event.

What triggers blur event?

The blur event fires when an element has lost focus. The event does not bubble, but the related focusout event that follows does bubble. The opposite of blur is the focus event, which fires when the element has received focus. The blur event is not cancelable.

How do you write Onblur in HTML?

The onblur attribute fires the moment that the element loses focus. Onblur is most often used with form validation code (e.g. when the user leaves a form field). Tip: The onblur attribute is the opposite of the onfocus attribute.

Can we apply Onblur 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.


2 Answers

I realize this was over a year ago, but it showed up for me in Google while trying to solve this same issue.

It seems Chrome does not consider some elements, like body and anchors, to be "focusable". According to the following page, you can make any element focusable (thereby activating the blur event) by setting the tabindex attribute: http://www.quirksmode.org/dom/events/blurfocus.html

Like so:

<a href="#" tabindex="0" onblur="alert('blur')"> blur me </a>
like image 148
jkoreska Avatar answered Oct 18 '22 20:10

jkoreska


Just ran into this issue...

onBlur (with a capital "B") doesn't work, but onblur (lowercase "b") does. Go figure! ;-/

like image 1
Jabari Avatar answered Oct 18 '22 20:10

Jabari