0

is giving this error Uncaught TypeError: Cannot read property 'style' of null in the following code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Documento sem título</title>
<style>
#div{
    width:100px;
    height:100px;
    background:#999;
    -webkit-transform:none;
}
</style>
<script type="text/javascript">
deg1=0;
deg2=0;
function animando(){
    deg1++;
    deg2++;
    quadrado = document.getElementById('div');  
    quadrado.style.webkitTransform = "rotate(-2deg)";
    }
setInterval(animando(),100);
</script>
</head>

<body>
<div id="div">

</div>
</body>
</html>
0

2 Answers 2

2
setInterval(animando(),100);

should be

setInterval(animando, 100);

without the (). You're trying to actually CALL the animando function in your code, so when the setInterVal call is prepping its arguments, the actual <div id="div"> element has not been parsed yet, and therefore the getElementById() call returns null because the ID doesn't exist (yet).

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

Comments

0
setInterval(animando(),100);

should be

setInterval(animando, 100);

without the (). You're trying to actually CALL the animando function in your code, so when the setInterval call is prepping its arguments, the actual <div id="div"> element has not been parsed yet, and therefore the getElementById() call returns null because the ID doesn't exist (yet).

Comments

Your Answer

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