0

My script wont run or the document.getElementById("toMove"); doesn't seem to catch the element. Its very basic javascript but I cant understand why its not working.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>test</title>
    <style>
    #toMove
    {
        background-color:lightblue;
        position:absolute;
    }
    </style>
        <script>
        var elem = document.getElementById("toMove");
        elem.style.margin = "600px";
    </script>   
</head>
    <body>  
<div id="toMove" style="margin:200px; font-size:26px;"> Hello World</div> 
    </body> 

Thanks

2
  • Noticed the </head ? Commented Jan 19, 2014 at 12:45
  • Yes, thanks. That was just a copy paste error. The main problem was that the element was not in the DOM yet as pointed out bellow. Commented Jan 19, 2014 at 13:04

5 Answers 5

1

Close your the tag properly. Problem is that when your scripts executes your element toMove does not exists in DOM

Wrap your code in window.onload. The function execute a JavaScript when a page is finished loading.

 window.onload = function(){
    var elem = document.getElementById("toMove");
    elem.style.margin = "600px";
 }

OR, Simply write your script at after you HTML declartion

Sign up to request clarification or add additional context in comments.

Comments

1

The problem is that this script must run once the DOM is ready (all elements rendered in the browser) Wrap the script like described here:

pure JavaScript equivalent to jQuery's $.ready() how to call a function when the page/dom is ready for it

Or if you use jquery with

$('document').ready(function(){});

Comments

0

There's a couple of reasons why it's not working

  1. You need to close the <head> tag properly
  2. The point at which your script runs, the element with the id toMove does not yet exist in the DOM (Document Object Model)

Solving number 1 should be easy enough, just change </head to </head>. To solve number 2, you need to ensure that the script fires after the element with id toMove exists in the DOM. You can do this by hooking up your script to fire when the window has finished loading. The code below sets up a function to fire when the window has finished loading

window.onload = function(){
   var elem = document.getElementById("toMove");
   elem.style.margin = "600px";
}

Comments

0

You must close head tag correctly.

</head>

And also do the code of Satpal.

5 Comments

This is rather a comment to Satpal's answer
@RokoC.Buljan, He don't have enough reputation now, Hopefully he will get there
@RokoC.Buljan, its 50
Sorry, I can't make comments of publications of other people since I get 50 of reputation.
You will eventually get there mate :). I know that
0

This will work.

<!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <title>test</title>
        <style>
        #toMove
        {
            background-color:lightblue;
            position:absolute;
        }
        </style>

    </head
        <body>  
    <div id="toMove" style="margin:200px; font-size:26px;"> Hello World</div> 
        </body> 
         <script>
             var elem = document.getElementById("toMove");
              elem.style.margin = "500px";
        </script>  
        </html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.