Instead of querySelector you should use matches(). The matches() method in JavaScript is used to determine if an element would be selected by a specified CSS selector. This method returns true if the element matches the selector, and false otherwise.
Source code viewer
/* <div id="myDiv" class="container highlight">Hello, World!</div> */ const element = document.getElementById('myDiv'); console.log(element.matches('.container')); // true console.log(element.matches('.highlight')); // true console.log(element.matches('#myDiv')); // true console.log(element.matches('.nonexistent-class')); // falseProgramming Language: ECMAScript