From the course: JavaScript: Enhancing the DOM
Unlock the full course today
Join today to access over 22,400 courses taught by industry experts or purchase this course individually.
Choosing elements by HTML tag
From the course: JavaScript: Enhancing the DOM
Choosing elements by HTML tag
Traditionally, the easiest way to get to an element is by ID, but you probably don't have an ID for every element in the DOM, or at least you probably shouldn't. If getElementsByID is the most popular way to access things, then getElementsByTagName is the next most popular. GetElementsByTagName lets you access elements by their HTML tags. It's super simple to use and returns an array. So, notice that there is an extra S here. So, althought it's different than getElementbyId, it can be used with it to create complex targets. So let's take a look. So I'm going to come here to my documents and I'm going to pull up the console. So I'm going to hit Cmd+Option+I, and then I'm going to hit the Esc key. I'm on a Mac, so that would be Ctrl+Option+I on a PC. So from here I can type in something like documents get elements by tag name. And then ask for a specific tag. So I'll ask for the List Item tag, and hit Return. Notice that I get an array like element with a bunch of list items, and I can…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
Selecting elements with getElementById4m 10s
-
Choosing elements by HTML tag3m 20s
-
Isolating elements by class name3m 12s
-
Querying CSS to select elements4m 54s
-
Working with named form elements3m 39s
-
Understanding nodeType, nodeName, and nodeValue4m 30s
-
Traversing up and down DOM nodes4m 40s
-
Targeting node elements2m 44s
-
-
-
-
-