Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event bubbling and the onblur event

I'm writing a form validation script and would like to validate a given field when its onblur event fires. I would also like to use event bubbling so i don't have to attach an onblur event to each individual form field. Unfortunately, the onblur event doesn't bubble. Just wondering if anyone knows of an elegant solution that can produce the same effect.

like image 679
Stephen Sorensen Avatar asked Oct 06 '09 13:10

Stephen Sorensen


People also ask

What is meant by event bubbling?

Event bubbling is a type of DOM event propagation where the event first triggers on the innermost target element, and then successively triggers on the ancestors (parents) of the target element in the same nesting hierarchy till it reaches the outermost DOM element or document object (Provided the handler is ...

What is the difference between event bubbling and event capturing?

Event Capturing is opposite to event bubbling, where in event capturing, an event moves from the outermost element to the target. Otherwise, in case of event bubbling, the event movement begins from the target to the outermost element in the file.

What is event bubbling in react?

Event bubbling in React refers to when the innermost component handles an event, and events bubble outwards. In React, the innermost element will first be able to handle the event, and then surrounding elements will then be able to handle it themselves.

What is the difference between Onblur and Onchange?

onblur fires when a field loses focus, while onchange fires when that field's value changes. These events will not always occur in the same order, however. In Firefox, tabbing out of a changed field will fire onchange then onblur, and it will normally do the same in IE.


3 Answers

You're going to need to use event capturing (as opposed to bubbling) for standards-compliant browsers and focusout for IE:

if (myForm.addEventListener) {
    // Standards browsers can use event Capturing. NOTE: capturing 
    // is triggered by virtue of setting the last parameter to true
    myForm.addEventListener('blur', validationFunction, true);
}
else {
    // IE can use its proprietary focusout event, which 
    // bubbles in the way you wish blur to:
    myForm.onfocusout = validationFunction;
}

// And of course detect the element that blurred in your handler:
function validationFunction(e) {
    var target = e ? e.target : window.event.srcElement;

    // ...
}

See http://www.quirksmode.org/blog/archives/2008/04/delegating_the.html for the juicy details

like image 75
Crescent Fresh Avatar answered Oct 23 '22 19:10

Crescent Fresh


use 'Focusout' event as it has Bubble up effect..thanks.

like image 5
Umesh K. Avatar answered Oct 23 '22 18:10

Umesh K.


ppk has a technique for this, including the necessary workarounds for IE: http://www.quirksmode.org/blog/archives/2008/04/delegating_the.html

like image 2
NickFitz Avatar answered Oct 23 '22 18:10

NickFitz