I am relatively new to JS, but I programmed it in C before and I am trying to draw attention to this whole event. I am trying to create a script that will check the input of a form, however, all my code runs - everything inside the if / else loop, what you have - regardless of event or not. To test and facilitate shipping here, I wrote a simple program that also has this problem.
HTML:
<button id="test">Test</button>
JavaScript:
function init(){ document.getElementById("test").onclick = alert("hello"); } window.onload = init;
From what I understand, the init function should be called when the page loads (window.onload), and a warning appears when the button with the identifier "test" is pressed (onclick). In fact, what happens is that the warning appears as soon as the page loads, and will reappear if I press the button.
My thinking is that I made some incorrect assumptions about the order in which JS is executed, but I cannot think what it is. Can anyone shed some light on this?
source share