All language subtitles for 004 Selecting HTML Elements with Javascript.en

af Afrikaans
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
tl Filipino
fi Finnish
fr French Download
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:00,160 --> 00:00:01,370 Welcome back guys. 2 00:00:01,440 --> 00:00:07,800 So, as you saw in the previous lesson, we were able to use Javascript to select HTML elements and change 3 00:00:07,860 --> 00:00:09,450 its color property. 4 00:00:09,450 --> 00:00:15,180 Now I showed you one way of doing this using a method called querySelector, but there's actually a 5 00:00:15,180 --> 00:00:17,390 lot more ways that you can do this. 6 00:00:17,430 --> 00:00:23,940 Now I've added a few classes and ids to our current web page, so we can try out all of the different 7 00:00:23,940 --> 00:00:27,420 ways of selecting elements inside the DOM. 8 00:00:27,420 --> 00:00:37,140 Now the first one I want to show you is the getElementsByTagName, and what this does is that it looks 9 00:00:37,260 --> 00:00:43,710 through the web page and it searches for the element with a particular tag name. 10 00:00:43,710 --> 00:00:52,110 So, for example, if we wanted to get a list item as we have here, we have three list items inside a unordered 11 00:00:52,110 --> 00:00:53,130 list. 12 00:00:53,130 --> 00:00:59,550 If we run this line of code, getElementsByTagName, then you'll see that we actually get back an 13 00:00:59,550 --> 00:01:03,490 array that contains all three of our list items. 14 00:01:03,540 --> 00:01:08,550 And if you expand this you can see that each list item has a whole bunch of properties that you can 15 00:01:08,550 --> 00:01:10,820 tap into and you can manipulate. 16 00:01:11,070 --> 00:01:16,390 Now, the important thing about this method, and where it differs from the previous one that we used, 17 00:01:16,420 --> 00:01:22,710 which is querySelector, is, if you notice, the word elements is plural here, 18 00:01:22,800 --> 00:01:29,890 and what this means is that it's going to fetch all of the elements that have this particular tag name. 19 00:01:29,940 --> 00:01:32,220 So it's not just going to fetch one. 20 00:01:32,280 --> 00:01:36,960 Now this is important because when you try to manipulate one of these elements, 21 00:01:37,040 --> 00:01:42,970 so if we try to tap into the third list item, then we can simply say getElementsByTagName, 22 00:01:43,050 --> 00:01:51,390 and whereas before we just said .style.color equals, say, I don't know, purple, then this is not going to 23 00:01:51,390 --> 00:01:57,480 work, because, even though the error that you get is a little bit misleading, it says Cannot set property 24 00:01:57,510 --> 00:01:59,200 of undefined, 25 00:01:59,280 --> 00:02:05,700 and the problem is that we're trying to set the color purple to an array, which doesn't work because, 26 00:02:05,700 --> 00:02:10,770 remember, this part of the code gives us back this array with all three list items, 27 00:02:11,010 --> 00:02:15,040 and if we try to set its style property, that doesn't make any sense. 28 00:02:15,060 --> 00:02:22,110 So what we have to do instead is we have to select the item in the array that we want to change and 29 00:02:22,110 --> 00:02:26,540 remember when we select items in arrays we always use the square brackets. 30 00:02:26,580 --> 00:02:32,640 So if we wanted the third item, then we would put in the index number 2, and then now if we set 31 00:02:32,640 --> 00:02:36,080 style.color = "purple", then you can see that it's changed 32 00:02:36,100 --> 00:02:40,470 our third list item to a purple text. 33 00:02:40,500 --> 00:02:46,260 Now if you're at all confused about arrays then be sure to go back to the last module where we spoke 34 00:02:46,260 --> 00:02:52,850 about arrays in detail and you can get to grips with this idea before you head back and we continue. 35 00:02:52,930 --> 00:02:59,940 Now in addition to being able to tap into a particular element inside this array of items that all have 36 00:02:59,940 --> 00:03:04,890 the tag name Ii, we could also use some of the other things that we saw that you can do with arrays, namely, 37 00:03:04,890 --> 00:03:09,860 for example, if we wanted to know how many elements there were that have the tag name li, 38 00:03:10,200 --> 00:03:16,290 then we can simply generate the array of all of the items and then call .length to get the length property 39 00:03:16,620 --> 00:03:21,650 and that will tell us that there are 3 li items in total on this entire page. 40 00:03:21,750 --> 00:03:23,130 So that's pretty neat. 41 00:03:23,130 --> 00:03:28,790 Now, along this line of getElementBy, you can see that there's a whole bunch of other ones, 42 00:03:28,860 --> 00:03:33,150 and another useful one is getElementsByClassName. 43 00:03:33,180 --> 00:03:40,650 So what this does is it allows you to select elements based on the name of the class. So you can see 44 00:03:40,650 --> 00:03:42,120 here in our web page, 45 00:03:42,120 --> 00:03:44,370 I've given a class to some of these elements. 46 00:03:44,430 --> 00:03:48,530 So for example the button element has a class of btn. 47 00:03:48,690 --> 00:03:55,770 So we can select that button by saying document.getElementsByClassName, and then inside here 48 00:03:55,770 --> 00:03:59,060 we'll add the class btn. 49 00:03:59,190 --> 00:04:04,650 Now notice this method also has a plural, Elements. 50 00:04:04,650 --> 00:04:08,660 So that means that we're also going to get back an array. 51 00:04:08,820 --> 00:04:16,140 So it doesn't matter whether if you only have one item that has the class btn. It's still going 52 00:04:16,140 --> 00:04:18,020 to give you back an array. 53 00:04:18,090 --> 00:04:24,630 So that means that doing this also does not work. 54 00:04:24,630 --> 00:04:30,780 And so even if there's only one item that has that particular class that you're selecting, you still 55 00:04:30,780 --> 00:04:36,660 have to select the first item using the square brackets and using the index 0. 56 00:04:36,660 --> 00:04:45,000 Now the final one in this style of getElementsBy is the getElementById. And the keen eyed amongst 57 00:04:45,000 --> 00:04:49,980 you might have noticed that in this case the word Element is no longer plural, 58 00:04:50,130 --> 00:04:56,550 and so in fact using this method you only get back one item instead of an array, 59 00:04:56,730 --> 00:05:02,080 and the reason being, of course, that, on a single web page, every single id should be unique, 60 00:05:02,090 --> 00:05:05,430 so you should not have the same id on more than one element. 61 00:05:05,450 --> 00:05:13,190 So, for example, in our case we've got the id list for our unordered list, and we've got the id title for 62 00:05:13,240 --> 00:05:14,480 our h1. 63 00:05:14,510 --> 00:05:22,650 So if I select on that title id, then I will only get back a single item as opposed to an array of items. 64 00:05:22,730 --> 00:05:25,460 And in this case we're getting back our h1. 65 00:05:25,460 --> 00:05:31,790 So if I want to change the text inside that title, I can say simply select the element that has the id 66 00:05:31,790 --> 00:05:37,170 title, and then change its innerHTML to say, I don't know, Good Bye. 67 00:05:37,220 --> 00:05:42,500 Now that text has gone from Hello to Good Bye by selecting it and then manipulating it. 68 00:05:42,560 --> 00:05:48,080 Now, another method that can be used to select a single element is the querySelector method that we 69 00:05:48,080 --> 00:05:50,260 already saw in the last lesson. 70 00:05:50,270 --> 00:05:58,100 Now this method, similar to getElementById, also only returns a single item, but it works a little bit 71 00:05:58,100 --> 00:06:04,820 differently, because the string that you're going to put inside the parentheses is a selector. 72 00:06:04,820 --> 00:06:06,120 Now what are selectors? 73 00:06:06,170 --> 00:06:08,610 Well, we've been using it in our CSS 74 00:06:08,840 --> 00:06:10,110 all of this time. 75 00:06:10,220 --> 00:06:14,090 Here's the styles.css us for our TinDog web site. 76 00:06:14,270 --> 00:06:21,560 And you can see that the selector that we use in CSS is just the part that we specify before the curly 77 00:06:21,560 --> 00:06:22,280 braces. 78 00:06:22,430 --> 00:06:27,950 So to select elements we simply just write the name of the element, to select classes 79 00:06:27,980 --> 00:06:34,880 we add a dot in front of the name of the class, and to select ids we add the pound sign in front. 80 00:06:35,180 --> 00:06:39,510 And of course with selectors we can combine different things, 81 00:06:39,530 --> 00:06:46,910 for example an id with a class, or a class with an element, and it's a much more specific way of selecting 82 00:06:46,910 --> 00:06:51,090 a particular thing inside your web site in order to change. 83 00:06:51,320 --> 00:06:57,900 So let's head back over here and let's use our querySelector to select something. 84 00:06:57,920 --> 00:07:01,660 So whereas before, when I used getElementById, 85 00:07:01,760 --> 00:07:07,790 I could only specify a valid id, and when it does getElementsByClass then I could only put in a class, 86 00:07:08,150 --> 00:07:08,860 inside here 87 00:07:08,870 --> 00:07:11,830 I could put in an element, a class, or an id, 88 00:07:11,840 --> 00:07:13,000 it doesn't really matter. 89 00:07:13,130 --> 00:07:21,260 So for example, if I want to select our h1, then I can simply say h1 in here, or if I wanted to 90 00:07:21,260 --> 00:07:29,570 select it by its id, then I could say querySelector, and in here I would add that pound sign to show 91 00:07:29,570 --> 00:07:35,870 that I'm specifying an id, then I would use the id title, and still we would get back exactly the same 92 00:07:35,870 --> 00:07:36,380 thing. 93 00:07:36,650 --> 00:07:43,340 And it works exactly the same way with class as well. All we have to do is just add a dot in front, just 94 00:07:43,340 --> 00:07:44,950 as we did inside the CSS. 95 00:07:44,960 --> 00:07:51,000 So if I wanted our button then I can just say .btn and I would get back our button. 96 00:07:51,140 --> 00:07:58,430 But more importantly, I can combine our selectors and query for something that's quite specific. 97 00:07:58,460 --> 00:08:03,040 So for example we've got a link here inside our first list item. 98 00:08:03,050 --> 00:08:07,340 Now let's say that we have another link that's just free floating around, 99 00:08:07,550 --> 00:08:12,630 and I only wanted to select the link that was inside the list item. 100 00:08:12,800 --> 00:08:19,220 Well, I can do that by combining my selector and saying select the anchor tag inside the list element. 101 00:08:19,560 --> 00:08:24,740 And remember when we're doing hierarchical selectors, there's a space between the two selectors. 102 00:08:24,740 --> 00:08:32,120 So now, if I hit enter, then you can see that I'm getting back the href that's inside our li, as opposed to 103 00:08:32,180 --> 00:08:33,140 this one, 104 00:08:33,140 --> 00:08:37,460 whereas if I just said querySelector("a"), then I would get back 105 00:08:37,460 --> 00:08:42,860 this first one. As you can see the highlighting is showing you which one we're getting back from these 106 00:08:42,860 --> 00:08:44,410 results. 107 00:08:44,540 --> 00:08:51,680 Now I can also combine my selectors by saying something like I want the element that has a class of 108 00:08:51,740 --> 00:08:54,760 item that is also an li element. 109 00:08:54,800 --> 00:09:00,780 So to do that I would say li. to specify the class which is called item. 110 00:09:00,950 --> 00:09:03,410 And remember when you're combining selectors, 111 00:09:03,410 --> 00:09:06,160 so things that occur in the same element, 112 00:09:06,200 --> 00:09:08,210 then there are no spaces. 113 00:09:08,210 --> 00:09:14,750 So whereas before it was the anchor element inside the list element, 114 00:09:14,780 --> 00:09:20,580 so hierarchical, the child of this parent, in this case it's all in the same element. 115 00:09:20,660 --> 00:09:26,240 It's the list item that also has a class of item. 116 00:09:26,270 --> 00:09:33,920 So now if I hit enter you can see I get back my first list item, and you can also use the id as well. 117 00:09:33,920 --> 00:09:39,550 So, for example, in this case, what do you think I will select based on this selector? 118 00:09:39,560 --> 00:09:41,800 Five seconds, see if you can get the answer. 119 00:09:46,170 --> 00:09:46,530 All right. 120 00:09:46,530 --> 00:09:52,420 So this is a hierarchical selector, as you can tell by the space in between the two selectors. 121 00:09:52,680 --> 00:09:59,810 So in this case we're looking for an anchor tag that is inside something that has an id of list. 122 00:09:59,850 --> 00:10:07,580 So, in that case, that is going to be this one, which is a child, or rather grandchild, of this unordered 123 00:10:07,590 --> 00:10:09,390 list with an id of list. 124 00:10:09,840 --> 00:10:13,150 And if I hit enter we can confirm the answer. 125 00:10:14,590 --> 00:10:20,890 Now here's a question. What if your selector matches more than one object? 126 00:10:20,890 --> 00:10:30,280 Because if I say, for example, id list with a class of item, then you could see that all three of these 127 00:10:30,280 --> 00:10:33,000 list items satisfy that query. 128 00:10:33,010 --> 00:10:40,780 They all have a class of item and they're all nested under a ul with an id of list. 129 00:10:40,780 --> 00:10:42,970 So which one do you get back? 130 00:10:43,150 --> 00:10:50,960 Well actually, you only get back the first item in the document that satisfies that selector. 131 00:10:50,990 --> 00:10:57,610 Now, what if you wanted all of the objects that match that selector? Well, then you would have to use a 132 00:10:57,610 --> 00:11:04,080 slightly different method. Instead of querySelector, which sounds very singular, 133 00:11:04,360 --> 00:11:07,300 you would have to use a method called querySelectorAll. 134 00:11:07,420 --> 00:11:15,820 And now, by using the exact same line of code other than that word All, we get back a list of all 135 00:11:15,820 --> 00:11:19,790 three list items that satisfy that particular selector. 136 00:11:19,930 --> 00:11:21,240 And similar to before, 137 00:11:21,250 --> 00:11:28,360 if you wanted to change and manipulate items in here, you would have to specify their index, because it 138 00:11:28,360 --> 00:11:30,100 is an array. 139 00:11:30,100 --> 00:11:37,060 Now, given that we've seen all of these different types of ways of selecting our HTML elements, which 140 00:11:37,060 --> 00:11:38,850 one should you actually use? 141 00:11:38,950 --> 00:11:45,820 Well, querySelector, and it's slightly greedier brother querySelectorAll, allow for more complex 142 00:11:45,820 --> 00:11:54,100 queries, because we're able to specify id, class, element, tag names, and combine them in order to target 143 00:11:54,100 --> 00:11:56,270 the exact element that we want, 144 00:11:56,440 --> 00:12:02,740 whereas the getElement methods are more broad and it's more difficult to target individual objects without 145 00:12:02,740 --> 00:12:04,640 going in to change the HTML. 146 00:12:04,660 --> 00:12:10,240 So, generally, in the code that you'll come across in the wild, you'll see querySelector a lot more than 147 00:12:10,240 --> 00:12:17,440 getElement, as, in most cases, you can use querySelector and querySelectorAll to do all the things that 148 00:12:17,440 --> 00:12:17,990 you can do 149 00:12:18,000 --> 00:12:19,950 with the getElement methods. 150 00:12:20,290 --> 00:12:26,260 So I recommend that you try out all of them and you'll get a feel for which one will be the most adaptable 151 00:12:26,590 --> 00:12:31,150 and which one you'll find to be the most useful in your case. 152 00:12:31,150 --> 00:12:38,620 Now as a challenge I want you to use what you learned in this lesson to change the text color of the 153 00:12:38,620 --> 00:12:40,450 Google link over here. 154 00:12:40,450 --> 00:12:43,150 So not this one but this one. 155 00:12:43,150 --> 00:12:51,680 Now remember that, if you look at the DOM tree, that link is an anchor tag nested inside a list element, 156 00:12:51,790 --> 00:12:58,770 so try and see if you can change its color to red. Pause the video and complete the challenge now. 157 00:12:58,810 --> 00:12:59,170 All right. 158 00:12:59,170 --> 00:13:00,720 So how did that go? 159 00:13:00,970 --> 00:13:08,580 Well, remember that the anchor tag that we want to change is nested inside an li inside a ul. 160 00:13:08,590 --> 00:13:14,590 So in order to change the color of our Google link we have to select the anchor tag and not just the 161 00:13:14,590 --> 00:13:16,210 list item. 162 00:13:16,210 --> 00:13:25,240 So to do that you have to tap into the document and then we have to querySelector for a anchor tag that is 163 00:13:25,330 --> 00:13:27,860 inside a list element. 164 00:13:28,350 --> 00:13:30,380 And now we can use this. 165 00:13:30,410 --> 00:13:33,160 And now let's just confirm that we've got the right one. 166 00:13:33,260 --> 00:13:38,980 There we go. And the anchor tag that we want is being highlighted, so we can now go and simply set its 167 00:13:39,340 --> 00:13:42,700 style.color to red. 168 00:13:42,700 --> 00:13:43,350 There we go. 169 00:13:43,510 --> 00:13:46,000 And now that link is red. 170 00:13:46,060 --> 00:13:53,140 Now if you tried to do this just by selecting the first list item then this will only change the color 171 00:13:53,140 --> 00:13:58,030 of the bullet point without changing the color of the text, 172 00:13:58,420 --> 00:14:03,920 and that is because this anchor tag is actually a separate element from the list item. 173 00:14:04,000 --> 00:14:08,820 So maybe that was a little bit mean on my part but I hope it was fun trying it out nonetheless. 174 00:14:08,830 --> 00:14:15,140 So in the next lesson we're going to learn more about how we can manipulate the CSS styles using Javascript. 175 00:14:15,170 --> 00:14:16,940 So I'll see you on the next lesson. 18744

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.