filmov
tv
Event bubbling in JavaScript
Показать описание
Link for all dot net and sql server video tutorial playlists
Link for slides, code samples and text version of the video
Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.
What is event bubbling
Let us understand this with an example. HTML elements can be nested inside each other. For example a button element can be nested inside a span element and the span element inturn can be nested inside a div element.
Notice that we have onclick attribute specified for all the 3 elements.
[html]
[head]
[style type="text/css"]
.styleClass
{
display: table-cell;
border: 1px solid black;
padding: 20px;
text-align: center;
}
[/style]
[/head]
[body]
[div class="styleClass" onclick="alert('Div click handler')"]DIV element
[span class="styleClass" onclick="alert('Span click handler')"]Span element
[input type="button" value="Click me" onclick="alert('Button click handler')" /]
[/span]
[/div]
[/body]
[/html]
A click on the button, causes a click event to be fired on the button. The button click event handler method handles the event. The click event then bubbles up to the button element parent (span element), which is handled by the span element event handler method. The click event then bubbles up to the span element parent (div element). This is called event bubbling.
Notice that if you click on the [span] element, it's event handler and it's parent([div]) element event handler are called. If you click on the [div] element, just the [div] element event handler method is called. So, the event bubbling process starts with the element that triggered the event and then bubbles up to the containing elements in the hierarchy.
The following example is similar to the one above, except we removed the onclick attribute from button and span elements. Because of event bubbling, when you click on the button or the span element, the event gets handled by the div element handler.
[html]
[head]
[style type="text/css"]
.styleClass
{
display: table-cell;
border: 1px solid black;
padding: 20px;
text-align: center;
}
[/style]
[/head]
[body]
[div class="styleClass" onclick="alert('Click event handled by DIV element')"]DIV element
[span class="styleClass"]Span element
[input type="button" value="Click me"/]
[/span]
[/div]
[/body]
[/html]
When the event gets bubbled, the keyword this references the current element to which the event is bubbled. In the example below, we are using "this" keyword to reference the current div element and change it's border color. When you click on the innermost [div] element, all the 3 [div] elements border get changed due to event bubbling.
[html]
[head]
[style type="text/css"]
.divStyle
{
display: table-cell;
border: 5px solid black;
padding: 20px;
text-align: center;
}
[/style]
[/head]
[body]
[div id="DIV1" class="divStyle"]
DIV 1
[div id="DIV2" class="divStyle"]
DIV 2
[div id="DIV3" class="divStyle"]
DIV 3
[/div]
[/div]
[/div]
[script type="text/javascript"]
divElements[i].onclick = function () {
}
}
[/script]
[/body]
[/html]
Stopping event bubbling : If you don't want the event to be bubbled up, you can stop it.
With Internet Explorer 8 and earlier versions
With Internet Explorer 9 (and later versions) & all the other browsers
Link for slides, code samples and text version of the video
Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.
What is event bubbling
Let us understand this with an example. HTML elements can be nested inside each other. For example a button element can be nested inside a span element and the span element inturn can be nested inside a div element.
Notice that we have onclick attribute specified for all the 3 elements.
[html]
[head]
[style type="text/css"]
.styleClass
{
display: table-cell;
border: 1px solid black;
padding: 20px;
text-align: center;
}
[/style]
[/head]
[body]
[div class="styleClass" onclick="alert('Div click handler')"]DIV element
[span class="styleClass" onclick="alert('Span click handler')"]Span element
[input type="button" value="Click me" onclick="alert('Button click handler')" /]
[/span]
[/div]
[/body]
[/html]
A click on the button, causes a click event to be fired on the button. The button click event handler method handles the event. The click event then bubbles up to the button element parent (span element), which is handled by the span element event handler method. The click event then bubbles up to the span element parent (div element). This is called event bubbling.
Notice that if you click on the [span] element, it's event handler and it's parent([div]) element event handler are called. If you click on the [div] element, just the [div] element event handler method is called. So, the event bubbling process starts with the element that triggered the event and then bubbles up to the containing elements in the hierarchy.
The following example is similar to the one above, except we removed the onclick attribute from button and span elements. Because of event bubbling, when you click on the button or the span element, the event gets handled by the div element handler.
[html]
[head]
[style type="text/css"]
.styleClass
{
display: table-cell;
border: 1px solid black;
padding: 20px;
text-align: center;
}
[/style]
[/head]
[body]
[div class="styleClass" onclick="alert('Click event handled by DIV element')"]DIV element
[span class="styleClass"]Span element
[input type="button" value="Click me"/]
[/span]
[/div]
[/body]
[/html]
When the event gets bubbled, the keyword this references the current element to which the event is bubbled. In the example below, we are using "this" keyword to reference the current div element and change it's border color. When you click on the innermost [div] element, all the 3 [div] elements border get changed due to event bubbling.
[html]
[head]
[style type="text/css"]
.divStyle
{
display: table-cell;
border: 5px solid black;
padding: 20px;
text-align: center;
}
[/style]
[/head]
[body]
[div id="DIV1" class="divStyle"]
DIV 1
[div id="DIV2" class="divStyle"]
DIV 2
[div id="DIV3" class="divStyle"]
DIV 3
[/div]
[/div]
[/div]
[script type="text/javascript"]
divElements[i].onclick = function () {
}
}
[/script]
[/body]
[/html]
Stopping event bubbling : If you don't want the event to be bubbled up, you can stop it.
With Internet Explorer 8 and earlier versions
With Internet Explorer 9 (and later versions) & all the other browsers
Комментарии