From the course: JavaScript: Ajax and Fetch
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Select elements with vanilla JavaScript - JavaScript Tutorial
From the course: JavaScript: Ajax and Fetch
Select elements with vanilla JavaScript
- [Instructor] Before you can make changes to an element in the DOM, you first have to select it. The DOM interface supported by browsers includes several methods for selecting one or more elements, but a few methods are particularly common. querySelector takes a CSS selector as an argument and selects a single element. querySelectorAll is similar to querySelector except that if your CSS selector matches multiple elements, all of them are returned as a collection. There are also older methods such as getElementById, which selects the element with the ID value you pass. In general, querySelector and querySelectorAll are more versatile, but you may see getElementById in legacy code. Looking at our Explore California form, remember our first goal is to collect address, city, and state info, and then use that to retrieve and display zip info. So we'll need to reference those four form fields in the DOM. Now, when you use the method like querySelector, the browser needs to do some work to…
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
-
-
-
-
What is the DOM?3m 19s
-
Select elements with vanilla JavaScript3m 43s
-
Request data in response to an event4m 14s
-
Add an event listener2m 12s
-
Work with JSON data6m 52s
-
Modify form values with vanilla JavaScript4m 58s
-
Modify element content with vanilla JavaScript5m 49s
-
Challenge: Modify the DOM58s
-
Solution: Modify the DOM7m 55s
-
-
-
-
-