I have an HTML table and want the body of the table to be filled in upon clicking a submit button from another HTML file in the same folder.
TABLE
<table class="tbl-upcoming">
<thead>
<th>Game</th>
<th>Pick</th>
<th>Odds</th>
<th>Confidence</th>
</thead>
<tbody>
<!--JAVASCRIPT FUNCTION FILLS OUT TABLE-->
</tbody>
</table>
SUBMIT BUTTON WITH INPUTS THAT ARE TO BE SUBMITTED
<div class="popup-content">
<h2>My Bet</h2>
<div class="content-line">
<span>Pick:</span>
<p class="choice" id="bet-choice"></p>
</div>
<div id="choice-odds" class="content-line">
<span>Odds:</span>
<input type="text" class="choice" id="odds-choice">
</div>
<div id="choice-conf" class="content-line">
<span>Confidence (1-10):</span>
<input type="number" class="choice" id="conf-choice" min="0" max="10">
</div>
<button class="close-btn" onclick="togglePopup()">×</button>
<button class="submit-btn" onclick="addBet()">Submit</button>
</div>
JAVASCRIPT
var list1 = [];
var list2 = [];
var list3 = [];
var n = 1;
var x = 0;
function addBet() {
var betChoice = document.getElementById("bet-choice").innerHTML;
var oddsChoice = document.getElementById("odds-choice").innerHTML;
var confChoice = document.getElementById("conf-choice").innerHTML;
document.getElementById("popup-1").classList.toggle("active");
var table = document.querySelector('.tbl-upcoming');
var newRow = table.insertRow(n);
list1[x] = betChoice;
list2[x] = oddsChoice;
list3[x] = confChoice;
var cell1 = newRow.insertCell(0);
var cell2 = newRow.insertCell(1);
var cell3 = newRow.insertCell(2);
cell1.innerHTML = list1[x];
cell2.innerHTML = list2[x];
cell3.innerHTML = list3[x];
n++;
x++;
}
The table remains unchanged after I click submit. Here is the error I get when I inspect the console
Uncaught TypeError: Cannot read properties of null (reading 'insertRow')
at addBet (app.js:22:24)
at HTMLButtonElement.onclick (bet-page.html:103:67)
Any ideas on what I'm doing wrong?