From the course: JavaScript: Enhancing the DOM

Unlock the full course today

Join today to access over 24,000 courses taught by industry experts.

The matches method

The matches method

- [Instructor] Before we move on to this module's challenge, let's talk about a lesser known but useful method, matches. Matches doesn't return elements like query selector and query selector all. Instead, it checks if an element would be selected by a specific CSS selector, it returns true or false. So here's the webpage we're going to use. You can see that some of them have the class active and that line 11 to 14 are inside something called class content. Well imagine you have an element stored in the variable, such as here, we have a node list with all the elements inside and elements with the class content. In order to check whether some of those inner elements have the class active, we can use the matches method. And here's how to do that. I'm going to say if a certain element matches, and then here I'm going to get the class active. Well, in that case, I want to print the element. So go ahead, say console log, found active elements, and then followed by the element itself. So…

Contents