JS working different depending on location

I am using this tutorial to create a boolean that is toggled by a button. When using the JS in an html file with the html elemnt it works fin but if I use the JS in a .js file and then call the code I get this error message .

Hey, I just tested it out myself and it works for me.

Can you provide a link to your project?

FYI, it’s not really recommended to call functions this way. Another option is to use an event listener. Here’s an example:

// script.js
let testBool = true;
console.log("Default value of bool is", testBool);

const toggleBtn = document.querySelector("#toggle");

function toggle() {
  testBool = testBool ? false : true;
  console.log("Toggled bool is", testBool);

toggleBtn.addEventListener("click", toggle);

// index.html
<!--<button onclick="toggle()">-->
<button id="toggle">Toggle Bool</button>
1 Like

Here is a video showing how to replicate the issue. https://youtu.be/5RTaGRMBMdc My site is at dev.flench.me or http://dev-flench.glitch.me and my code is at https://glitch.com/edit/#!/dev-flench?path=README.md%3A1%3A0.