I know, there are many threads covering this topic. BUT I didn't figure out yet, why the console is still showing the error.
The Plan
When I click on input#submit
, I want main_jobBot.js
to switch from index.html
to https://www.google.de/
.
Summary
I am trying to call the function change
by the btn.onclick
event.
It works fine, when I place the OnClick Event inside of the <input>
tag.
But when I place it directly into the main_jobBot.js
, I get the following Error:
ERROR MESSAGE:
Uncaught TypeError: Cannot set property 'onclick' of null at main_jobBot.js:7
I have...
- ...checked the link to the
main_jobBot.js
. - ...tried to move the script to the end of the
index.html
in order to make sure, every element was loaded, that themain_jobBot.js
might need. - ...tried to rename all my components to make sure, there is no default variable or method, I accidentally used.
- ...double checked the code for typing errors.
Note: I am an absolute beginner to Javascript as well as to Stackoverflow!
MAIN_JOBBOT.JS and INDEX.HTML
/* main_jobBot.js */
let btn = document.getElementById("submit");
function change() {
location.replace("https://www.google.de/");
}
btn.onclick = change;
<!-- index.html -->
<input type="submit" value="Let J.A.B. find the Job!" id="submit">
</input>
FINAL EDIT: Now I've got it! I had to remove the #
AND move the script to the End of the HTML file!