0

I'm trying to link the JavaScript file to the HTML in this specific way, but it's not working. Not interested in using jQuery for this specific question or referencing the function in html.

// javascript.js
const button1 = document.querySelector(".button1");

button1.onclick = function() {
  alert("External JS file works!");
}
<input type="button" value="button1" class="button1"></input>

Clicking the button doesn't show the alert

Umutambyi Gad
  • 4,082
  • 3
  • 18
  • 39
wongz
  • 3,255
  • 2
  • 28
  • 55
  • 2
    Just like with inline JS, you need the DOM to be ready. Either place the ` – blex Jul 04 '20 at 21:57
  • Since the code you posted works as evinced by the example above, either your path to the external JS file is incorrect, or you're executing your code too early – j08691 Jul 04 '20 at 22:01
  • Hm... it's so weird. I have another button and function where I make it alert via and that works but the one in the question doesn't. So the js file is loaded and referenced properly but something else is off that I'm missing I think... – wongz Jul 04 '20 at 22:10

0 Answers0