Search code examples
javascriptbuttongetelementbyid

Only first button works. Not sure if its the script. It might be I'm missing a rule


I know I'm a beginner at this, but I really need to learn the rules. I'm trying to make a simple example of javascript. Its a box with three buttons, but for some reason only one of them works. They do have different ID types, so there must be another rule I don't know.

<!DOCTYPE html>
<html>
<head>

    <title>Javascript box</title>

</head>
<body>

    <p>Press buttons to change the box</p> 

    <div id="box" style="height:100px; width:100px; background-color: limegreen; margin:50px"></div>

    <br>

    <button id="less">less</button>

    <button id="more">more</button>
    
    <button id="none">Reset</button>


    <script type="text/javascript">

        document.getElementById("less").addEventListener("click", function(){

            document.getElementById("box").style.height = "25px";
    
        });

        document.getElementByID("more").addEventListener("click", function(){

            document.getElementById("box").style.height = "150px";
            
        });
   
        document.getElementById("none").addEventListener("click", function(){

            document.getElementById("box").style.height = "100px";
            
         
        });

     </script>

</body>

</html>


Solution

  • For the second addEventListener, there's a typo. In document.getElementByID, the D should not be capitalised.
    A good way to debug these issues is to look at the console in developer tools.