Javascript DOM Element class name set

Description

Javascript DOM Element class name set

View in separate window


<!DOCTYPE html>

<html>
<head>
    <style type="text/css">
    .message-style1 {
        color: navy;
        font-weight: bold;
        background-color: gray;  
    }//from   www . j a  v a 2s. c o  m
    </style>
</head>
<body>
    <div id="divMessage">
        Here is a message.
    </div>
    <input type="button" value="Toggle Style" onclick="toggleStyle()" />
    <script type="text/javascript">
    function toggleStyle() {
        let divMessage = document.getElementById("divMessage");

        if (divMessage.className === "message-style1") {
            divMessage.className = "";
        } else {
            divMessage.className = "message-style1";
        }
    }
    </script>
</body>
</html>



PreviousNext

Related