JavaScript DOM Change Position of an Element

document object model it is interface for web documents

<div id="box" style="width: 50px; height: 50px; background: green; position: absolute; top: 50px; left: 50px;"></div>
<button onclick="moveRight()">Move Right</button>
<script>
    function moveRight() {
        let box = document.getElementById("box");
        let left = parseInt(box.style.left) || 0;
        box.style.left = (left + 50) + "px";
    }
</script>

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top