problem: the page executes the javascript onclick propertise before get clicked.
here is my code
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>demo</title>
<meta charset="UTF-8"/>
</head>
<body>
<h1 id="demo">Hello World!</h1>
<script>
var x = document.getElementById("demo");
x.onclick = alert("clicked");
</script>
</body>
</html>
solution: when I change the line x.onclick = alert("clicked");
to
x.onclick = function() {alert("clicked")};
It works.
I want to understand the reason behind this behavior of onclick.