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

Select elements with vanilla JavaScript - JavaScript Tutorial

From the course: JavaScript: Ajax and Fetch

Start my 1-month free trial

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…

Contents