I am trying to create a simple phone book in javascript. I want when the user clicks on "Add Contact" to have their name and phone appear in a separate division at the bottom. I have the code below, but for some reason it's not working. What are your thoughts?
var persons = {};
function showTable() {
str = "";
for (var i = 0; i < persons.length; i++) {
str += `<tr>
<td>${persons[i].name}</td>
<td>${persons[i].phone}</td>
</tr>`
}
document.querySelector("table tbody").innerHTML = str;
}
<!DOCTYPE html>
<html>
<head>
<title>PhoneBook</title>
</head>
<body>
<h1>PhoneBook</h1>
<form>
<label for="name">Name</label>
<input type="text" name="name" id="name">
<label for="phone">Phone</label>
<input type="text" name="phone" id="phone">
<input type="button" class="btn" value="Add contact" onclick="showTable;">
</form>
<div id="containerPhoneBook">
<table id="inputs">
<thead>
<tr>
<td>Name</td>
<td>Phone</td>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</body>
</html>
onclickshould beonclick="showTable()". Also, you are not getting the text from the input boxes anywhere in your code