External JS not working in html file help please

When I use script tag in body js work fine
after that I write code in external js file not work…
I also try same code in body script tag work file then the code copy and past in external js after that not working see picture…

js code
// const loginBtn = document.getElementById(“login”);
// loginBtn.addEventListener(“click”, function() {
// console.log(“hello Arnab”);
// })

the script in the head will work before the page is loaded so you can’t use addEventListener on something is not there yet so either you call the script after the element has loaded or add window.onload() to your code :

window.onload = function(){//your code}

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.