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.

Selecting elements with getElementById

Selecting elements with getElementById

From the course: JavaScript: Enhancing the DOM

Start my 1-month free trial

Selecting elements with getElementById

In this chapter I'm going to go over the different methods for selecting DOM elements. By far the most common is the getElementById method. It allows you to select and element in the HTML file that is a tag that has a single ID attribute. getElementById() expects to find only one ID since an ID should appear only once in each document. One common error that happens when accessing DOM elements is that people type in getElementsById(), with an s, so make sure you watch out for that. I really got to use a console.dir instead of console log. It can help you find the methods and properties available to every node in the DOM. So lets take a look at the DOM for a website in the console. I got this page opened up on a browser and I am going to hit Cmd+Option+I to enter the developer tools in Google chrome. Now I'm in the Console tab but you can switch to the Element tabs if you want to and take a look at the structure of this document. It's pretty simple. Just an HTML document with a body…

Contents