The problem I am having is that when I add a new actor the info displays in different rows. Take a look at the onclick function.
I need to add a new actor and display it in the table just like the first two are displayed.
var actors, i;
var Info = [{
firstName: "Jason",
lastName: "Statham",
birth: "July 26, 1967",
gender: "Male",
genre: "Action, Crime, Thriller"
}, {
firstName: "Mark",
lastName: "Wahlberg",
birth: "June 5, 1971",
gender: "Male",
genre: "Action, Comedy, Drama"
}];
var displayActors = function(actors) {
var str = "<table class='table'>";
str += "<tr>";
str += "<th>First Name</th><th>Last Name</th><th>Date of Birth</th><th>Gender</th><th>Genre</th>";
str += "</tr>";
for (i = 0; i < actors.length; i++) {
str += "<tr>";
str += "<td>" + actors[i].firstName + "</td>";
str += "<td>" + actors[i].lastName + "</td>";
str += "<td>" + actors[i].birth + "</td>";
str += "<td>" + actors[i].gender + "</td>";
str += "<td>" + actors[i].genre + "</td>";
str += "</tr>";
}
str += "</table>";
document.getElementById("actorGrid").innerHTML = str;
}
window.onload = function() {
displayActors(Info);
}
var sub = document.getElementById("submit").onclick = function() {
var fName = document.getElementById("fname").value;
var lName = document.getElementById("lname").value;
var dOb = document.getElementById("dob").value;
var str = "";
str += "<td>" + Info.push({
firstName: fName
}) + "</td>";
str += "<td>" + Info.push({
lastName: lName
}) + "</td>";
str += "<td>" + Info.push({
birth: dOb
}) + "</td>";
//Info.push({firstName: fName}, {lastName: lName}, {birth: dOb});
document.getElementById("actorGrid").innerHTML = str;
displayActors(Info);
}
<label>First Name</label><br />
<input type="text" id="fname"/><br />
<label>Last Name</label><br />
<input type="text" id="lname"/><br />
<label>Date of Birth</label><br />
<input type="date" id="dob"/><br />
<form action="">
<label>Gender:</label><br />
Male<input type="radio" name="gender" value="Male" id="male"/>
Female<input type="radio" name="gender" value="Female" id="female"/><br />
</form>
<form action="">
<label>Genre:</label><br />
<label>Action<input type="checkbox" id="action"/></label>
<label>Adventure<input type="checkbox" id="adventure"/></label>
<label>Thriller<input type="checkbox" id="thriller"/></label>
<label>Drama<input type="checkbox" id="drama"/></label>
</form>
<br /><input type="button" id="submit" value="Add Actor" />
<hr>
<div id="actorGrid"></div>