Regards, I can not figure out how to enter the table in the section it belongs to. As you can see, it prints me one below the other, and I want to create within the created "tr" and so many "td" how many fields I have in the input field. Using this code, he creates "tr" and within it there is only one 'td' and so for both input fields, but I want to create a 'tr' for each click, in which two fields are placed in 'td' separately .
My table: https://prnt.sc/kyi54g
const form = document.querySelector('form');
const table = document.querySelector('tbody');
const button = document.querySelector('#clear-button');
const inputName = document.getElementById('name');
const inputAge = document.getElementById('age');
// Zamenjuje if..else (if = ?) (else = :)
let itemsArray = localStorage.getItem('items') ? JSON.parse(localStorage.getItem('items')) : [];
localStorage.setItem('items', JSON.stringify(itemsArray));
const data = JSON.parse(localStorage.getItem('items'));
const thMaker = (text) => {
const tr = document.createElement('tr');
const td = document.createElement('td');
td.innerHTML = text;
table.append(tr);
tr.appendChild(td);
// var row = table.insertRow(-1);
// var cell1 = row.insertCell(0);
// var cell2 = row.insertCell(1);
// cell1.innerHTML = text;
// cell2.innerHTML = text;
}
form.addEventListener('submit', function (e) {
e.preventDefault();
itemsArray.push(inputName.value);
itemsArray.push(inputAge.value);
localStorage.setItem('items', JSON.stringify(itemsArray));
thMaker(inputName.value, inputAge.value);
inputName.value = "";
inputAge.value = "";
});
data.forEach(item => {
thMaker(item);
});
<html>
<body>
<div id="addRow">
<form method="post" id="form-table">
String
<input type="text" id="name" name="text4">
Number
<input type="number" id="age" name="text5">
<input type="submit" name="submit"> <!-- Submit dugme - **** PROBLEM: Refreshuje stranicu **** -->
</form>
</div>
<!-- TABELA -->
<div id="download-excel">
<table id="myTable" class="t01">
<tr>
<th>
Name
</th>
<th>
Age
</th>
</tr>
</table>
</div>
</body>
</html>
<td>tags inside one<tr>.