DEV Community

daloke85
daloke85

Posted on

Uncaught TypeError: Cannot set properties of null (setting 'innerHTML')

<!DOCTYPE html>
<html>
<head>
<script>
var x=1;
function change()
{
if (x==1)
x=0;


else
x=1;
document.getElementById(test).innerHTML=x;
}
</script>
</head>
<body>
<div id=test></div>
<input type=button value=change onclick=change()>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

I write the above code to print the value of "x" but this code gives error

Image description
Why is it giving this error?****

Top comments (2)

Collapse
 
leerob97 profile image
Lee Robertson

If this is literal, I would assume its because you have test as a variable instead of as a string "test".

Collapse
 
daloke85 profile image
daloke85

Thanks, Now my code works fine.