Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
If you know the tag name of the element, you can use getElementsByTagName()
. This method returns an HTMLCollection of elements with the given tag name.
Further Reading and Related Videos
- document.getElementsByTagName() – MDN
- HTMLCollection – MDN
- HTMLCollection.length – MDN
- How to Loop Through an HTMLCollection
- Using a for Loop to Iterate Over an Array
Exercise
Inside the loop, the variable i
holds the current index value. Log items[i]
to the console to view the returned elements as the loop runs.
for (let i = 0; i < items.length; i++) {
items[i].style.color = 'orchid';
console.log(items[i]); // <li style="color: orchid;">Make coffee</li>
}
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up