Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't obj.style.left = "200px"; working in this code?

Why wont .style.top = "200px"; work in this code? Calling the event within the element using oclick="this.style.left='200px';" doesn't work either.

<html>
    <head>
    </head>
    <body>
        <div id="theDiv">The Div</div>
        <button id="theButton">Do</button>
    </body>
</body>
</html>
<script type="text/javascript">
    document.getElementById("theButton").onclick = function(){
        document.getElementById("theDiv").style.top = "200px";
    };
</script>
like image 528
Babiker Avatar asked Mar 03 '26 00:03

Babiker


1 Answers

To use a left or top style the element must be position:absolute, or position:relative, or position:fixed

like image 77
Josiah Ruddell Avatar answered Mar 04 '26 13:03

Josiah Ruddell