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
From the course: JavaScript: Enhancing the DOM
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…
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
-
-
-
-
-