Why is my JS not working?

(JS Fiddle for context: https://jsfiddle.net/qLry5cwx/2/) The js is supposed to display a modal but it appears to not even try to execute. The code still works in another project of mine, from which I ripped all of the code, but not on my new site. I cannot figure out for the life of me why it isn't working. Can someone help me? Cheers
5 Replies
Jochem
Jochem15mo ago
const downloadBtn = document.querySelector(".download-btn");
const downloadBtn = document.querySelector(".download-btn");
This isn't returning anything because there is no class="download-btn" The console says so:
"<a class='gotoLine' href='#867:1'>867:1</a> TypeError: downloadBtn is null"
"<a class='gotoLine' href='#867:1'>867:1</a> TypeError: downloadBtn is null"
Lord Of Insanity
Lord Of Insanity15mo ago
The download button is on a different page, but I don't see how that can affect the js for the modal
Jochem
Jochem15mo ago
because certain errors stop javascript execution the rest of the file never runs because you're trying to set a listener on a non-existent element
Lord Of Insanity
Lord Of Insanity15mo ago
How can I fix it then?
Jochem
Jochem15mo ago
gotta fix or prevent that error from occurring, then the rest will work again the quick and dirty method is to do this:
if (downloadBtn) downloadBtn.addEventListener("click", downloadAgain);
if (downloadBtn) downloadBtn.addEventListener("click", downloadAgain);
instead of just
downloadBtn.addEventListener("click", downloadAgain);
downloadBtn.addEventListener("click", downloadAgain);
Want results from more Discord servers?
Add your server
More Posts