I declared a variable box and want to change it´s style property. Somehow it´s not working and I get an error like this "can not set property'style' of null object. I don´t understand why this isn´t working. But when I put the variable inside the function it works.Does anyone else had the same problem and how do you fixed it?
I just notice it works inside Stack Overflow. But not in Google Chrome and IE.
var boxPosition = 50;
var boxSpeed = 5;
var limit = 300;
var box = document.getElementById('box');
function draw(){
box.style.left = boxPosition + 'px';
}
#box{
position: absolute;
top: 100px;
left: 100px;
width: 50px;
height: 50px;
background: blue;
}
<html>
<head>
<link rel = "stylesheet" href = "Style.css">
<script language="javascript" type="text/javascript" src="Motion.js"></script>
</head>
<body>
<div id = "box" onclick="draw();"></div>
</body>
</html>
`
– adeneo May 07 '17 at 18:50