Selectors API basics

The selectors API specification is designed to make selecting DOM elements a lot simpler. It works in a similar way to how JavaScript libraries such as jQuery select elements, and will make much more sense to non-JavaScript experts. The two main methods in the API are querySelector() and querySelectorAll(). The first one selects the first element in the DOM that matches the specified selector. For example, the following selects the first p element in a document:

document.querySelector("p");

The second method selects all of the matching elements in the DOM. For example, the following line selects all of the elements in a document with a class of alert:

document.querySelectorAll(".alert");