- Overview
- Transcript
6.3 Working With `NodeLists`
A NodeList
is a collection of elements. In this lesson, you'll learn how to work with them.
1.Introduction2 lessons, 06:25
1.1Introduction01:51
1.2What You Need04:34
2.How the Web Works2 lessons, 16:03
2.1Networks and DNS09:20
2.2HTTP and the Web Server06:43
3.Creating Documents3 lessons, 27:13
3.1Marking Up a Document09:38
3.2Making Our Document Valid06:22
3.3Semantic and Generic Elements11:13
4.Styling Documents3 lessons, 34:07
4.1Adding Style08:17
4.2CSS Selectors16:15
4.3Modifying Layout09:35
5.Scripting Documents3 lessons, 22:36
5.1Variables and Functions09:06
5.2Writing Your Own Functions05:34
5.3Objects07:56
6.The Document Object Model6 lessons, 42:45
6.1DOM Basics08:58
6.2Finding Elements07:40
6.3Working With `NodeLists`04:30
6.4Manipulating an Element's CSS Classes04:29
6.5Listening for Events08:42
6.6Practicing the Concepts08:26
7.HTTP Requests With JavaScript1 lesson, 09:16
7.1The fetch() API09:16
8.Introduction to Server-Side Development5 lessons, 43:24
8.1Introduction to Server-Side Development10:31
8.2Your First Line of PHP Code06:57
8.3Functions and Variables08:00
8.4Parameters and Decisions08:13
8.5Getting Data From the User09:43
9.Getting Started With Databases3 lessons, 31:01
9.1Create a Users Table12:00
9.2Create a Posts Table10:20
9.3Inserting Data08:41
10.Using PHP to Interact With MySQL4 lessons, 51:59
10.1Reading and Displaying Data12:34
10.2Passing and Validating Data11:58
10.3Updating Data14:52
10.4Joining Data (And Deleting Posts)12:35
11.Conclusion1 lesson, 01:47
11.1Conclusion01:47
6.3 Working With `NodeLists`
In the previous lesson you learned how to find elements in the document. So we looked at the getElementById, the querySelector, and the querySelectorAll methods. And they all worked, however, the last one, querySelectorAll, gave us a different type of object. GetElementById and querySelector just gave us an individual element that we could work with. The querySelectorAll method gives us what's called a node list. It is a collection of all of the elements that match our CSS query. Now let's first of all talk about why it's called a node list. And that's because inside of the document object model every type of object is a node. It is the smallest building block used by all of the objects in the document, like atoms are the smallest building block of just about everything. So because in the document object model everything is a node then a collection of nodes is called a node list. Now the way that we work with a node list has changed over many, many years to the point to where today it is very simple. Now the reason why it is simple is because the browsers that we have today have been programed to make it a little simple. But if you have to support an older browser, like Internet Explorer, hopefully you don't. Well, then things would be a little bit different. We're not going to focus on that, we're going to focus on the code that we can write today. So we want to change all of our span elements to the color blue. So to do that, first of all, we need to use our spanElements variable. This is our node list and since this is a collection, we can do something for each element in our node list. Now this forEach is a method and a method is nothing more than a function that is attached to an object. So we will want to execute forEach, which means that we need a set of parentheses after it. But the forEach method needs something to work with. It's going to work with the node list, but it also needs a function. It's something that we call a callbackfunction, and it's called a callbackfunction because this forEach method is going to call whatever function that we specify. If that doesn't make sense, hopefully this will. We're going to write a function called setColorBlue. And we need the element that we are going to be working with, that we want to set the color to blue. So the code is going to look almost exactly like we have on line 12 here, except that we are using a different object. So now that we have this function defined, all we have to do then is just pass that function to forEach. Now notice here, there is a distinct difference between using the name of the function and then executing the function with a set of parentheses afterwards. In this case, we do not want to execute the function. We want to pass this function object, which is what we have. Whenever you define a function, you have created a function object. We want to pass that function object to forEach, and then behind the scenes for every element in this node list, it's going to execute this setColorBlue. It's going to pass in the element and then that function will execute. So that whenever we save this over on the right hand side, we see that the text is now all blue. But let's do one other thing. We have this spanElement and let's change that to firstSpan. And let's set something unique here, not necessarily the color, but let's do something with the font weight just so that it stands out. And we will set that to bold. So we save that and once again, the firstSpan that it is selected with the querySelector method is bold, but then all of the span elements are set to have a blue color. So far we've been looking at how to set individual CSS properties using the style object. Well in the next lesson you will learn how we can manipulate style with classes.