Basic javascript on codePen

56 views Asked by At

In localhost, this very simple code has no issue but it doesn't work on codePen? (the color background should change when the button is clicked)

https://codepen.io/jmercier/pen/mdvzJEV

The hover states of my button doesn't work on codePen and the javaascript is ignored.

Why?

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Inter:wght@100;200;300;400;500;600;700;800;900&display=swap');

        :root {
            --bg-color: #F5F5F5;
            --text-color: #000;
            --bg-hover: green;
            --text-hover: white;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box
        }

        body {
            transition: background-color 2s;
            background: var(--bg-color);
            color: var(--text-color);
            font-family: 'Inter', sans-serif;
        }

        #container {
            display: flex;
            flex-direction: column;
            width: 100%;
            height: 100vh;
            justify-content: center;
            align-items: center;
        }

        .title {
            font-weight: 400;
            font-size: 1.5rem;
        }

        .sstitle {
            font-weight: 300;
            font-size: 0.825rem;
            letter-spacing: .5rem;
        }

        .btn {
            border: none;
            padding: 0.5rem 1rem;
            margin-top: 1rem;
            background: var(--text-color);
            color: var(--bg-color);
        }

        .btn:hover {
            background: var(--bg-hover);
            color: var(--text-hover);
            cursor: pointer;
        }
    </style>
</head>

<body>
    <div id="container">
        <p class="title">Hello World</p>
        <p class="sstitle">Welcome</p>
        <button class="btn" id="btn">Changing Color</button>
    </div>
    <script>
        let state = 0;
        let btn = document.getElementById("btn");
        btn.addEventListener("click", toogleLight)
        function toogleLight() {
            if(state===0){
                document.documentElement.style.setProperty("--bg-color","black");
                document.documentElement.style.setProperty("--text-color","#F5F5F5");
                state = 1;
            } else{
                document.documentElement.style.setProperty("--bg-color","#F5F5F5");
                document.documentElement.style.setProperty("--text-color","black");
                state = 0;
            }
        }
    </script>
</body>
</html>

the color background should change when the button is clicked

0

There are 0 answers