HTML - DOMTokenList contains() Method



HTML DOMTokenList contains() method returns a boolean value representing whether the DOMTokenList contains the specified token or not.

Syntax

domtokenlist.contains(token);

Parameter

This method accepts a single parameters as listed below.

Parameter Description
token It represents name of the token which you want to check in the DOMTokenList.

Return Value

It returns a boolean value where true represents list contains the token specified and false represents list does not contains the token.

Example of HTML DOMTokenList 'contains()' Method

In the following example, we have checked for whether this document contains class='font' or not.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        HTML DOMTokenList add() Method
    </title>
    <style>
        .color {
            background-color: #04af2f;
            color: white;
        }
        .font {
            font-size: 40px;
        }
        .align {
            text-align: center;
        }
    </style>
</head>
<body>
    <p>Hii..</p>
    <p id="add" class="color font align">
        Welcome to Tutorials Point...
    </p>
    <p>
        Check whether the document has your desired class:
    </p>
    <button onclick="fun()">Click me</button>
    <p id="token"></p>
    <script>
        function fun() {
            let x = document.getElementById("add").classList.contains("font");
            document.getElementById("token").innerHTML = x;
        }
    </script>
</body>
</html>

Supported Browsers

Method Chrome Edge Firefox Safari Opera
contains() Yes 8 Yes 12 Yes 3.6 Yes 5.1 Yes 12.1
html_dom.htm
Advertisements