1 00:00:00,000 --> 00:00:06,490 Let's take a look at how we can dynamically add a class to an existing element. 2 00:00:06,560 --> 00:00:09,000 So I have some CSS up here, 3 00:00:09,000 --> 00:00:10,800 and let's get rid of that. 4 00:00:10,800 --> 00:00:14,580 I just have a box with a 100 height 100 background-color black. 5 00:00:14,580 --> 00:00:18,360 Let's go ahead and change that class or add a class 6 00:00:18,360 --> 00:00:22,580 rather to change that background color to be, let's say blue. 7 00:00:22,575 --> 00:00:24,885 So we could say there's a class called blue, 8 00:00:24,885 --> 00:00:29,475 and it's simply going to have a background color of blue. 9 00:00:29,475 --> 00:00:34,935 Now, I need this box elements to actually exist. 10 00:00:35,240 --> 00:00:38,250 And in my jQuery here, 11 00:00:38,250 --> 00:00:43,680 I can simply say dot box, 12 00:00:43,675 --> 00:00:47,565 dot add class blue. 13 00:00:47,560 --> 00:00:49,630 And before I do that, actually, 14 00:00:49,630 --> 00:00:52,030 let's go ahead and let's just work with classes for a little bit. 15 00:00:52,030 --> 00:00:54,790 Let's work with instead of IDs that's worked with classes. 16 00:00:54,790 --> 00:00:57,620 So we've got class box, 17 00:00:57,624 --> 00:01:02,124 box, box, box, and let's change that to dot box. 18 00:01:02,730 --> 00:01:05,020 So now when I refresh my page, 19 00:01:05,020 --> 00:01:07,530 my black box turns blue and now it's, 20 00:01:07,525 --> 00:01:09,015 you can kind of see, 21 00:01:09,010 --> 00:01:11,360 let it flashes real quickly. 22 00:01:11,400 --> 00:01:14,890 But it changing my box to be blue. 23 00:01:14,890 --> 00:01:20,410 And we can actually see this in as a real example here is we can delete this inner DOM. 24 00:01:20,410 --> 00:01:22,860 And this is what it looks like by default. 25 00:01:22,855 --> 00:01:26,495 And all we're simply doing is adding blue to it. 26 00:01:26,495 --> 00:01:31,295 Now, let's go ahead and wrap this in some sort of timeout. 27 00:01:31,295 --> 00:01:33,935 So let's do set timeout. 28 00:01:33,935 --> 00:01:37,595 And we could give this a function. 29 00:01:37,595 --> 00:01:41,105 And let's make it timeout for two seconds. 30 00:01:41,260 --> 00:01:45,100 And then select the box and add the class blue. 31 00:01:45,095 --> 00:01:47,405 So when I refresh the page here, 32 00:01:47,405 --> 00:01:49,735 it's going to wait just two seconds, 33 00:01:49,730 --> 00:01:51,960 and then it's going to turn to blue. 34 00:01:53,260 --> 00:01:56,120 Now if we want to write modern JavaScript, 35 00:01:56,120 --> 00:01:57,680 we don't need to use a function there. 36 00:01:57,680 --> 00:02:01,190 We can simply use an arrow function that's going to do the exact same thing, 37 00:02:01,190 --> 00:02:03,880 refresh, wait two seconds, it turns blue. 38 00:02:03,875 --> 00:02:08,045 We can also wrap this in any sort of event listener that we want. 39 00:02:08,045 --> 00:02:15,875 So we could say something like the button when it's clicked. 40 00:02:17,290 --> 00:02:20,040 Function. 41 00:02:20,950 --> 00:02:24,020 Or because we don't need the this keyword, 42 00:02:24,020 --> 00:02:26,340 we can do an arrow function. 43 00:02:27,220 --> 00:02:30,280 And let's get rid of that and move that up. 44 00:02:30,275 --> 00:02:33,235 And because we have a button event listener, 45 00:02:33,230 --> 00:02:34,690 we needed button on the page. 46 00:02:34,685 --> 00:02:37,585 So let's go ahead and add that button. Button. 47 00:02:37,580 --> 00:02:38,990 Click me. 48 00:02:38,990 --> 00:02:40,790 And when we click this button, 49 00:02:40,790 --> 00:02:44,630 is going to select that box and add a class of blue. 50 00:02:44,630 --> 00:02:46,370 Let's go ahead and refresh. 51 00:02:46,370 --> 00:02:47,720 Click. 52 00:02:47,720 --> 00:02:50,840 And something is not right here. 53 00:02:50,840 --> 00:02:53,360 And that's because I type clicked, 54 00:02:53,360 --> 00:02:55,310 it's supposed to be click. 55 00:02:55,310 --> 00:02:58,210 So refresh. Click. There we go. 56 00:02:58,205 --> 00:02:59,795 Refresh, click, it changes, 57 00:02:59,795 --> 00:03:01,375 refresh, click, it changes. 58 00:03:01,370 --> 00:03:04,880 We can do all sorts of things here and we could change the width as well. 59 00:03:04,880 --> 00:03:07,370 So it's a width 100, 60 00:03:07,370 --> 00:03:10,730 and this is simply going to change the width to be 100%. 61 00:03:10,730 --> 00:03:12,790 And so we can add class blue. 62 00:03:12,785 --> 00:03:19,195 We can also add class width 100 and refresh. 63 00:03:19,195 --> 00:03:23,425 And it changes its to be width of 100%. 64 00:03:23,425 --> 00:03:26,355 I want you to give this a shot. Try this. 65 00:03:26,350 --> 00:03:27,940 This is going to be very, very important. 66 00:03:27,940 --> 00:03:32,440 You're going to be using this a lot in vanilla JavaScript as well as regular jQuery. 67 00:03:32,439 --> 00:03:35,339 So you're going to want to be able to add a class. 68 00:03:35,335 --> 00:03:38,415 You can add multiple classes by simply adding a space in there. 69 00:03:38,410 --> 00:03:42,420 And jQuery will figure it out and add those classes for you. 70 00:03:42,415 --> 00:03:46,555 Wrap that in an event listener if you do not need the, 71 00:03:46,555 --> 00:03:50,155 this keyword, I'm saying this like this. 72 00:03:50,155 --> 00:03:52,155 If you don't need that inside of your function, 73 00:03:52,150 --> 00:03:54,450 you can simply write an arrow function as well. 74 00:03:54,445 --> 00:03:55,965 It's just a little more performance, 75 00:03:55,960 --> 00:03:57,630 a little more modern JavaScript. 76 00:03:57,625 --> 00:04:02,095 And then you can select your box or your element. 77 00:04:02,095 --> 00:04:04,425 And then you can add a class of whatever your classes are. 78 00:04:04,420 --> 00:04:06,880 So you are going to need to know a little bit of CSS for this. 79 00:04:06,880 --> 00:04:09,360 If you don't really know CSS, that's okay. 80 00:04:09,355 --> 00:04:12,925 You can just pause the video here and copy what I have on my screen. 81 00:04:12,925 --> 00:04:16,225 I simply have a box, box class. 82 00:04:16,225 --> 00:04:19,405 I've got a blue class, any width 100 class. 83 00:04:19,405 --> 00:04:23,275 And all that's doing is going to be adding these classes. 84 00:04:23,275 --> 00:04:26,565 And again, when we inspect, 85 00:04:26,560 --> 00:04:31,840 we now have class box blue and width 100. Go ahead and give that a shot. 86 00:04:31,840 --> 00:04:33,900 And when we are ready, 87 00:04:33,895 --> 00:04:35,485 when we're nice and ready. 88 00:04:35,485 --> 00:04:38,565 Let's go ahead and take this exact example and do 89 00:04:38,560 --> 00:04:42,560 the exact opposite where we remove a class.