I am getting the error: “Uncaught TypeError: Cannot read property ‘data1’ of null”
This page tells me what is wrong: http://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element
I have implemented the suggestions and still the error persists. I need another set of eyes:
First, here’s the essential setup:
<form id="userInput" action ="" method="GET">
<input class="buttonClass" type="button" id="importIt" value="Save Data" onclick="persistData(this.form)">
<!-- No response with this: <input type="button" class="buttonClass" id="importIt" value="Save Data"> -->
<input class="inputClass" id="data2" type="text" value="" placeholder="Body">
At bottom of page:
<script src="../js/setups-import.js" defer></script>
<!-- <script>importIt.addEventListener('click', persistData);</script> -->
<script>dropDatabase.addEventListener('click', dropDb);</script>
External setups-import.js:
function persistData(data1, data2) {
// get form entries
var form = document.querySelector("#userInput");
// OLD: var form = document.getElementById("userInput");
var formdata1 = form.data1.value; // ERROR POINTS TO THIS LINE
var formdata2 = form.data2.value;
Steps I’ve taken:
- Make sure the userInput ID is spelled correctly everywhere.
- Put the JS after the HTML, just before /body, then as an external script.
- Added “defer” to the script at bottom of the page.
- Used eventListener at bottom of page instead of onclick on the buttons.
- Changed getElementById to querySelector.
What am I missing? I think I covered all the bases. I’m sure it is something ridiculously simple.