I have a form that the user can fill and print the table when they are done. I can not seem to validate if the user finished. Once the user finished populating the table, I would like the whole table to be printed. Right now it is only printing the last entry.
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type="text/javascript">
function validateForm()
{
var x = document.getElementById('fname').value;
if(x === null || x === "")
{
document.getElementById('error1').innerHTML = "Invalid Entry";
}
else
//tableCreate();
addMore();
}
function addMore()
{
if(confirm("Would you like to add more names?") === true)
{
document.getElementById('theForm').reset();
//tableCreate();
//console.log("ADD MORE");
}
else
tableCreate();
}
function tableCreate()
{
var N = document.getElementById('fname').value;
var L = document.getElementById('lname').value;
var D = document.getElementById('dob').value;
var ar = [N, L, D];
console.log(ar);
document.write('<table>');
document.write('<tr>');
document.write('<th>First Name</th>');
document.write('<th>Last Name</th>');
document.write('<th>Date of Birth</th>');
document.write('</tr>');
document.write('<tr');
for(var i = 0; i < ar.length; i++)
{
document.write('<br><td>' + ar[i] + '</td>');
}
document.write('</tr>');
document.write('</table>');
}
</script>
</head>
<body>
<form name="theForm" action="FormAndTable.html" method="post">
First Name: <input type="text" id="fname">
<span style="color:red" id="error1"></span><br>
Last Name: <input type="text" id="lname"><br>
Date of Birth: <input type="text" id="dob"><br>
<input type="button" value="Save" onclick="validateForm()">
</form>
</body>
</html>
else /*tableCreate();*/ addMore();. When you commented outtableCreate(),addMore()was moved into theelsestatement. Not sure if this was desired.