Recently I have noticed something strange in my friends code. At first I thought that it was a mistake, but then I tested it and it seems to work. Please check out this simple code (notice "button_test.click()"):
<html>
<head>
<script>
var test = function(){
alert('hello');
}
</script>
</head>
<body>
<button id="button_test" onclick="test();">click!</button>
<script>
button_test.click();
</script>
</body>
</html>
Why DOES it work? Where the "button_test" object is defined? I thought that I have to write document.getElementById("button_test") or $("#button_test") using jQuery to get a HTML element, but it seems to be unnecessary. Interestingly I get message "ReferenceError: button_test is not defined" when I move "button_test.click();" line to header.