You do not have access to this lesson!
Enroll in the training to get access to this lesson along with hundreds of other lessons and complete courses.
Already enrolled? Login
Course content
Lessons #1: Getting Started with jQuery PAID
10:21 mins
Lessons #2: Find Something / Query the DOM with jQuery - part 1 PAID
12:52 mins
Lessons #3: Find Something / Query the DOM with jQuery - part 2 PAID
22:25 mins
Lessons #4: Find Something / Query the DOM with jQuery - part 3 PAID
14:24 mins
Lessons #5: Do Something with jQuery PAID
11:39 mins
Lessons #6: Events with jQuery PAID
19:47 mins
Lessons #7: Project: Create a Top Bar Widget with jQuery PAID
23:47 mins
Lessons #8: Project: Create Tabs functionality with jQuery PAID
10:46 mins
Lessons #9: Animate: Animating elements with jQuery PAID
9:41 mins
Lessons #10: Event Delegation with jQuery PAID
4:52 mins
Lessons #11: Event Propagation with jQuery PAID
6:20 mins
Lessons #12: Prevent Default Behavior (of elements) with jQuery PAID
3:06 mins
Lessons #13: Content Manipulation with jQuery PAID
9:32 mins
Lessons #14: Keyboard Event with jQuery PAID
7:13 mins
Lessons #15: Project: Create your own Image Carousel Slider with jQuery PAID
27:07 mins
Lessons #16: Ajax: Create a Single Page App with jQuery PAID
15:09 mins
Lessons #17: Ajax: Create a Location Finder App using jQuery & Google Maps API - Part 1 PAID
9:53 mins
Lessons #18: Ajax: Create a Location Finder App using jQuery & Google Maps API - Part 2 PAID
4:18 mins
Event Propagation with jQuery
In this lesson, we are going to discuss Bubbling / Event Propagation Continuing from our previous Event Deligation lesson.
This time I'm going to select the second anchor tag which is named bubbling in our HTML document. I'm going to write a.bubbling and on click it should run a function and make the text bold
$(document).ready(function(){
$('a.bubbling').on('click', function(){
$(this).css('font-weight', 'bold');
})
});
Save it, refresh the screen and you will see the text is now bold if I click on it
I'm going to write another event handler, and this time I'm going to attach the click handler to the div with a class of main-class element and change the background color to black and the font color to white like such
$(document).ready(function(){
$('.main-class').on('click', function(){
$(this).css({
'background-color': 'black'),
'color': 'white'
})
})
});
Save it, refresh the screen and now if I click on the background area, any part of the main content the background color is going to change to black and the color of text will be white as shown belowAnother thing I want to show is that if you refresh the page and now you click on the Bubbling / Event Propagation the link will be bold and the background color will be changed to black. Save it, refresh the screen and you see, even though I clicked on the second link but the background color also changed.
You just learned the concept of the DOM, The Document Object Model. When you click on any of the child element, it takes the effect whatever its parent has. So in our case the second link is the child of the div element with a class of content which is inside the div element with a class of main-class as shown below
If you want to prevent it, you can write the function called
e.stopPropagation
like such $(document).ready(function(){
$('a.bubbling').on('click', function(e){
$(this).css('font-weight', 'bold');
e.stopPropagation();
})
});
Save it, refresh the screen and now if you click on the second link, it will become bold but the background and the rest will remain the same as shown belowYou notice we wrote
e
in the brackets and also wrote e.stopPropagation
. I'm going to console.log(e)
to see what this e
is.So
e
is a call back object that gets resolved when the event gets triggered. But it's not compulsory to call it e
you can call it anything you like and it will return the same results in the console. But since it's an event object, it's a good practice to call it e
You see below it has so many properties, you don't have to get in to detail, but it has so many properties on what got triggered or what was the properties of the event that got triggered.
If I click on the first link, the propagation will take place because there's no stopPropagation in the first event handler.
I hope you enjoyed this lesson, if you have any question, leave your comments below. I'll talk to you in the next lesson. Good bye :)
Become a confident software developer with mentor support!
Get Started
Already have an account? Please Login
Get Started
Already have an account? Please Login