HTML DOM matches() Method

The matches() method checks if the Element that would be selected by the provided selectorString is the selector. If the specified element is the valid selector then it will return true, else false.

Syntax:

var boolean_result = element.matches("selector");

Parameters: Here, selector is used as a parameter.

Return value: It returns Boolean value i.e. (True when selector matches else false).

Example: In this example, there is a list in which there are some elements that have class selector as number. So, we stored the list in a variable and by iterating on the list, if selector matches number class, then printing on the document correspondingly.



HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        HTML DOM matches() Method
    </title>
</head>
  
<body>
    <h1>GeeksforGeeks</h1>
  
    <ul id="abc">
        <li>A</li>
        <li class="number">1</li>
        <li>C</li>
        <li class="number">2</li>
    </ul>
  
    <script>
        var abc = document.getElementsByTagName('li');
        for (var i = 0; i < abc.length; i++) {
            if (abc[i].matches('.number')) {
                document.write(abc[i].textContent 
                    + ' is not an alphabet ,  ');
            }
        }
    </script>
</body>
  
</html>

chevron_right


Output:

Note: It is supported by all the browsers.

full-stack-img

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.