Skip to content
Advertisement

I tried moving my JS script into a separate js file, but it stopped working once I did. How can I fix it?

I just started learning JS, and im not understanding it that well. I am not that great at coding generally, so apologies for this really barebones and very possibly wrong website, but all I wanted to accomplish at the moment was to change the websites background color when I press the button. When I did it in <script< it worked, but when I moved it into a separate JS file, it stopped working. The error message I get is: SyntaxError: Unexpected token ‘}’. Expected an opening ‘{‘ at the start of a function body. Could someone please help? Thank you in advance!

JavaScript
JavaScript
JavaScript

Advertisement

Answer

you are assigning both btnRed and the document.getElementById method to a String value 'btnRed' instead of actually running the getElementById method and passing it an argument "btnRed"

so just change the let btnRed = document.getElementById = ('btnRed'); to let btnRed = document.getElementById('btnRed');

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement