I have this code that has an onclick
handler and an IIFE.
var btn = document.getElementById("btn");
btn.onclick = function clickHandler(){console.log("Button clicked"); }
//btn.onclick = function clickHandler(){console.log("Now clicked"); };
(function(){
console.log("Hello");
})();
The issue is that the clickHandler gets invoked automatically after the page loads and the IIFE isn't invoked. However, on commenting out line 2, and un-commenting out line 3 (the commented out line in the above code), the code works as expected.
The only difference between the two lines is that the 2nd line has no ; in the end, but the 3rd line has.
Also, if I remove the IIFE and keeping the rest of the code unchanged, the code works fine.