0

I wrote a simple code to learn Eventhandling in JavaScript but when I click on the button "Click" the function hello() does not work, why?

html file:

<!DOCTYPE html>
<html>
  <head>
    <script src="hello.js"></script>
  </head>
  <body>
    <button id="click">Click</button>
  </body>
</html>

JavaScript file:

    document.getElementById("click").onclick = hello;
    function hello() {
      alert("You Clicked!");
     }
newlearner
  • 149
  • 1
  • 11

1 Answers1

1

You are calling the script before the dom is generated. experiment to call the right before the tag.

Lhew
  • 584
  • 9
  • 22