0

Me, a HTML and JS newbie, has a simple piece of code that was almost literally copied from other answers when I was seeking for a solution for my problem (only changed the variables' names), yet it doesn't work in my case. The simplified version:

<!DOCTYPE html>
<html>
  <head>
    <title>my question</title>
    <script src="/myScript.js"></script>
  </head>

  <body>
    <form id="form1" action="#" method="post">
      <input id="text1">
      <input id="submit1" type="submit">
    </form>
  </body>
</html>

It does not alert() neither if I write it like this:

// myScript.js
document.querySelector("form").addEventListener("submit", function() {
  alert("test");
});

nor like this:

// myScript.js
document.querySelector("form").addEventListener("submit", myFunc);

function myFunc() {
  alert("test");
}

I can't figure out how should I fix it.

bl1te
  • 1
  • 1
  • using `document.getElementById("form1")...` (and other similar methods) instead of `document.querySelector("form")...` also changes nothing. I am using Chrome 87.0.4280.141 if it means something – bl1te Jan 14 '21 at 18:49
  • Your script is running before the element exists, check console for errors – Patrick Evans Jan 14 '21 at 18:50
  • Probably your axript is not loaded wäre is it located relative to the HTML? – niklas Jan 14 '21 at 18:50
  • @PatrickEvans , thank you, it gave me the direction to find a fix – bl1te Jan 14 '21 at 19:11

0 Answers0