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

Choosing elements by HTML tag

From the course: JavaScript: Enhancing the DOM

Start my 1-month free trial

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…

Contents