document.getElementById("myButton").onclick = alert('Hi!')
is wrong since onclick should be assigned to a function reference, not the function call result itself.
It will execute alert('Hi!')
when the page is loaded but that is not the intention here, is it? The intention behind assigning an onclick handler is to ensure that when the button is clicked this alert will be executed.
For that to happen it should be:
document.getElementById("myButton").onclick = function(){alert('Hi!')};
Also, this will not work unless it is wrapped inside the window.onload
event:
window.onload = function(){
document.getElementById("myButton").onclick = function(){alert('Hi!')};
};