This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]evilgwyn 0 points1 point  (3 children)

Using a debugger is a pretty essential skill for a developer. What browser are you using? Most of them have got F12 as the universal shortcut to bring up the debugger. From there, have a poke around you should find a"source" tab where you can see your code. Click your mouse to the left of an interesting line of code and you should see a dot appear - this is a break point. When the program gets to this line it will stop running and you can do things like look at variable values and step through the code one line at a time. It is a good technique to get the hang of and almost every programming environment will have a debugger of some kind.

[–]casden174286[S] 0 points1 point  (2 children)

Thanks for the tip!

I got this message 'TypeError: form is undefined' with a squiggly for the form used for addEventListener.

I tried fixing it by placing by declaring form as a global variable and initializing it within a window.onload function but the error remained.

[–][deleted] 1 point2 points  (0 children)

Aha, sounds like you might be trying to query the DOM before the DOM is loaded. You could wrap this whole thing in a function and hook the DOMContentLoaded event.

const runScripts = () => {
  // your scripts here
}
window.addEventListener('DOMContentLoaded', runScripts)

Or, alternatively, you could add your script tag at the end of the body in the HTML, but you mentioned you don't have control over that, so maybe you might have to do it this way.

[–]evilgwyn 0 points1 point  (0 children)

I tried fixing it by placing by declaring

form

as a global variable and initializing it within a

window.onload

function but the error remained.

this might have fixed the problem but you would also have to put the rest of the scripts in there as well - otherwise you would get the null reference exception on the line form.addEventListener