1
00:00:00,120 --> 00:00:05,430
Hey, this is going to be a short bonus lesson on bubbling and capturing, we talked about it a little

2
00:00:05,430 --> 00:00:06,020
bit earlier.

3
00:00:06,030 --> 00:00:07,680
Well, we have our event handling that.

4
00:00:07,680 --> 00:00:09,660
We have an option to do true or false.

5
00:00:09,960 --> 00:00:14,200
And this lesson will give you a little bit more of an illustration of how it works.

6
00:00:14,610 --> 00:00:17,400
So by default, you don't have to include it anymore.

7
00:00:17,410 --> 00:00:20,640
You did have to before and by default will go to false.

8
00:00:20,850 --> 00:00:25,350
And it only really makes a difference when you have two events on the same element.

9
00:00:25,650 --> 00:00:31,410
So there might be a case where you've attached an event listener to the parent, but then the children

10
00:00:31,410 --> 00:00:33,260
also have events as well.

11
00:00:33,600 --> 00:00:37,900
And when you are adding these event listeners, you have the option to capture or bubble.

12
00:00:37,920 --> 00:00:43,590
So the difference between Katcher and bubble bubbling is the event is first captured and handled by

13
00:00:43,590 --> 00:00:49,530
the innermost element and then propagated to the outer elements in capturing is the opposite, where

14
00:00:49,530 --> 00:00:54,240
the event is first captured by the outermost element and propagated to the inner element.

15
00:00:54,270 --> 00:00:59,760
So what we're going to do in this lesson is show up, show you how you can set this up and how you can

16
00:00:59,760 --> 00:01:01,770
explore a little bit more about how it works.

17
00:01:02,160 --> 00:01:06,450
So creating an event later on, elements are going to create a bunch of divs.

18
00:01:06,480 --> 00:01:08,480
These debates are going to be inside each other.

19
00:01:08,700 --> 00:01:13,620
And as you can see, five is inside of four, which is inside a three, which is inside a two, which

20
00:01:13,620 --> 00:01:14,920
is inside of one.

21
00:01:15,150 --> 00:01:19,470
So all these elements are going to be contained within each other and they're also going to have event

22
00:01:19,470 --> 00:01:19,920
listeners.

23
00:01:19,920 --> 00:01:23,370
So that's going to have multiple event listeners on the same events.

24
00:01:23,530 --> 00:01:27,780
And I show you how it gets handled using the bubbling and capturing.

25
00:01:27,960 --> 00:01:32,940
So the code snippet is this one here where we've got all of the device and as we can see, the device

26
00:01:33,210 --> 00:01:40,080
one is inside or one is the main parent and then the next device inside, next one inside, inside,

27
00:01:40,080 --> 00:01:40,560
inside.

28
00:01:40,560 --> 00:01:45,060
And then we close off all the device and we've got section ID where we've got some output.

29
00:01:45,270 --> 00:01:48,540
So this is where we can track what's happening with the bubbling.

30
00:01:48,810 --> 00:01:52,410
We select all of the elements and we loop through.

31
00:01:52,440 --> 00:01:58,470
We add event listeners, so we add one capture, one bubble, and that's where we've got the function

32
00:01:58,470 --> 00:01:58,920
names.

33
00:01:58,920 --> 00:02:03,570
So they can be named anything, of course, because they're just functions and one is set to true and

34
00:02:03,570 --> 00:02:04,530
one is set to false.

35
00:02:04,710 --> 00:02:05,870
So that's the big difference here.

36
00:02:05,880 --> 00:02:09,930
That true and false, and this will clearly illustrate what the difference is.

37
00:02:10,290 --> 00:02:15,140
So let's try this out and open up our editors and create a fresh document.

38
00:02:15,150 --> 00:02:17,010
We're going to create all the different diffs.

39
00:02:17,400 --> 00:02:22,080
So we've got our first div and within here we're just going to have a value of one.

40
00:02:22,440 --> 00:02:27,360
Inside of that one, we're going to have another div and this one will have a value of two.

41
00:02:27,360 --> 00:02:33,690
And then inside of this div will have another div and this one will have a value of three.

42
00:02:34,020 --> 00:02:41,160
So I'm not sure really the best way to kind of output this in the HTML, but we have another div inside

43
00:02:41,160 --> 00:02:45,090
of here and I know it's kind of getting a little bit tight here on the screen.

44
00:02:45,270 --> 00:02:50,350
So we've got four and then we have another div and this one is five.

45
00:02:50,520 --> 00:02:54,210
So let me do a quick clean up of this so you can see it a little bit better.

46
00:02:54,360 --> 00:02:55,380
When we refresh it.

47
00:02:55,530 --> 00:03:00,000
All we see is one, two, three, four, five, because of course the device don't have any content

48
00:03:00,000 --> 00:03:00,940
in it as of yet.

49
00:03:01,380 --> 00:03:07,830
We're also going to create another area that we can create an output so we can call this one, give

50
00:03:07,830 --> 00:03:12,000
this one a section tag so that we don't get mistaken for the device.

51
00:03:12,900 --> 00:03:16,470
And this is going to be the only section tags we don't actually even have to give an ID.

52
00:03:16,890 --> 00:03:19,380
I'm going to just add the JavaScript in page.

53
00:03:19,380 --> 00:03:24,350
So right on this index page or just have the JavaScript on the page itself.

54
00:03:24,810 --> 00:03:28,700
So selecting the elements is the first thing that we want to do.

55
00:03:28,710 --> 00:03:34,940
So we've got our output elements and that should be const output elements.

56
00:03:34,950 --> 00:03:37,260
And then this is where we're going to select that element.

57
00:03:37,740 --> 00:03:46,110
We can use documents, query selector and we're going to select it by TAG and we have only one section

58
00:03:46,110 --> 00:03:47,670
so we can select that really easily.

59
00:03:48,180 --> 00:03:51,100
The other one, we need to select all of the elements.

60
00:03:51,120 --> 00:03:56,620
So just call it El's and documents using query selector all.

61
00:03:56,880 --> 00:04:02,160
So we're going to select all of the devs on the page so we can add all of the event listeners to that.

62
00:04:02,190 --> 00:04:06,150
Next, we're going to do a for loop and looping through.

63
00:04:07,200 --> 00:04:17,010
Letting X or Y equals zero and then looping through while ls length is less than I and will increment

64
00:04:17,010 --> 00:04:20,060
I by one, so we have a way out of our loop.

65
00:04:20,070 --> 00:04:22,140
And this is we're going to add all of the event listeners.

66
00:04:22,440 --> 00:04:28,590
But before we add event listeners, I want to actually add in some styles to these elements.

67
00:04:29,270 --> 00:04:35,460
A lot of times what I'll do is I'll set up a variable, so I'll call it L. And then within here I'll

68
00:04:35,460 --> 00:04:36,290
have Elles.

69
00:04:36,660 --> 00:04:38,670
So it looks a little bit cleaner when you do it this way.

70
00:04:38,850 --> 00:04:42,000
So now I can just reference the object as well.

71
00:04:42,480 --> 00:04:45,270
And we've got style and then border.

72
00:04:45,450 --> 00:04:51,330
So one pick solid red so that we can distinguish between all the different diffs.

73
00:04:51,360 --> 00:04:56,010
Also, El, it's always good practice to try adding in some styling.

74
00:04:56,010 --> 00:05:00,360
And of course I could have done this with scissors, but we do want to focus on JavaScript.

75
00:05:00,570 --> 00:05:08,070
So therefore a great way to practice JavaScript another style, add in some padding and this actually

76
00:05:08,070 --> 00:05:10,140
should be, I guess, not incrementing.

77
00:05:10,140 --> 00:05:11,510
So that's why it's only adding it to the one.

78
00:05:12,150 --> 00:05:17,700
So now we have something way more interesting and we see that we've got the same pattern that we were

79
00:05:17,700 --> 00:05:25,080
looking at within the example we can also set because this is an object so we can set values to it so

80
00:05:25,080 --> 00:05:27,180
we can have AI plus one.

81
00:05:27,360 --> 00:05:31,530
So that means that each one of these elements is now going to have kind of a hidden value there.

82
00:05:31,540 --> 00:05:35,100
We could also call it secret or something like that to make it more interesting.

83
00:05:35,580 --> 00:05:39,850
Adding an event listener event that we're listening for is a click.

84
00:05:40,140 --> 00:05:41,640
So this is going to be for the catcher.

85
00:05:42,270 --> 00:05:48,920
So make sure that you set this one to true and the other one is going to be for the bubble and calling

86
00:05:48,920 --> 00:05:52,290
in the function bubble and make sure you set this one to false.

87
00:05:52,740 --> 00:05:53,980
Otherwise, it won't work.

88
00:05:54,210 --> 00:05:55,820
So we've got our event listeners.

89
00:05:55,830 --> 00:05:58,410
And next, we need to create our functions.

90
00:05:58,980 --> 00:06:03,390
So we've got one function called bubble and then we have another function called capture.

91
00:06:03,390 --> 00:06:08,700
And within these functions, we're just going to update the output and I'll create a function in order

92
00:06:08,700 --> 00:06:10,260
to handle the output as well.

93
00:06:10,590 --> 00:06:12,660
So function we can call it output.

94
00:06:12,960 --> 00:06:15,540
It will take in value of message.

95
00:06:15,990 --> 00:06:21,870
And then with that message value, it's going to take the output element that we have on the pick and

96
00:06:21,870 --> 00:06:25,740
update the inner HTML with whatever content.

97
00:06:25,770 --> 00:06:33,120
So just add to that just a quick short format there and using a template, literal adding in the value

98
00:06:33,120 --> 00:06:35,280
of the variable message.

99
00:06:35,580 --> 00:06:37,360
And we can do a line break as well.

100
00:06:37,380 --> 00:06:38,640
And of course then the back.

101
00:06:38,820 --> 00:06:41,420
So ready to add in content into the message.

102
00:06:42,390 --> 00:06:44,610
So do a quick refresh, make sure there's no errors.

103
00:06:44,820 --> 00:06:51,630
So now we can output to the output function a message, some information, and in this case we're going

104
00:06:51,630 --> 00:06:52,860
to call it capture.

105
00:06:53,100 --> 00:06:58,230
And then the contents of what we're capturing is whatever is contained within this V so that's that

106
00:06:58,230 --> 00:06:59,340
value that we're set there.

107
00:06:59,370 --> 00:07:02,580
That's how we're just getting whatever value that's contained within there.

108
00:07:02,590 --> 00:07:05,880
We could also take the inter text as well if we wanted to.

109
00:07:05,880 --> 00:07:08,540
And actually this one should be bubble and this one is capture.

110
00:07:08,550 --> 00:07:09,630
So I just got them back.

111
00:07:09,630 --> 00:07:12,580
So I'll update that to be bubble and that one is capture.

112
00:07:12,630 --> 00:07:15,030
So let's try this and see what happens now.

113
00:07:15,030 --> 00:07:20,610
Whenever I click, you see that when I click on FI, what we're getting for capture.

114
00:07:20,610 --> 00:07:22,500
So we get all of the captures run.

115
00:07:22,800 --> 00:07:29,190
So starting at 1:00, so it starts at the outermost element and moves towards the innermost element,

116
00:07:29,520 --> 00:07:37,590
whereas the bubble starts at five over here and it goes through all the elements ending at one.

117
00:07:37,750 --> 00:07:43,470
So if we click one, we're only going to see one because one is it's also the outer and the inner.

118
00:07:43,620 --> 00:07:47,640
So that's why it doesn't really make any difference if you've only got the one event.

119
00:07:47,820 --> 00:07:51,270
But if we click number two, we're going to have two different events.

120
00:07:51,540 --> 00:07:55,560
So let me refresh that and I'll make the smaller I have it zoomed in quite a bit.

121
00:07:55,800 --> 00:07:59,730
So if I click two, you're going to see that on the capture.

122
00:07:59,760 --> 00:08:04,260
We're starting out one, two, and on the bubble we're doing two one.

123
00:08:04,620 --> 00:08:06,690
So that's the way that bubble and capture works.

124
00:08:06,690 --> 00:08:08,850
So let's take a little bit time to get used to it.

125
00:08:09,150 --> 00:08:16,080
And it is only really important now if you are adding multiple event listeners to the same element.

126
00:08:16,080 --> 00:08:17,460
So go ahead and try this out.
