Thanks for the reply.
I had to make a few changes to how I was using local storage with the objects in the table rows. With what I originally posted it was only storing the latest 'added' row, instead of every row that was added. I found out by running to console.
Heres what I changed it to.
Code:
var addActor = function() {
var newActor = {fName: firstN, lName: lastN, birthDate: birthdate, gender: gender, action: action, comedy: comedy, drama: drama, suspense: suspense, sciencefiction: sciencefiction, horror: horror}
// Creates a table of links for each added actor with an id based from # of actors
$("#actorsTable").append("<tr><td><a href='' onclick='deleteActor(this); updateActor(this);' class='update' data-idx='" + actors.length + "'>" + newActor.fName + " " + newActor.lName + "</a></td></tr> ");
actors.push(newActor);
alert(JSON.stringify(newActor));
$(".update").off("click");
$(".update").on("click", selectActor);
console.log(actors);
localStorage.setItem("lsActor", JSON.stringify(actors));
};
Code:
$(document).ready(function() {
var newActors2 = localStorage.getItem("lsActor")
console.log('newActors2', JSON.parse(newActors2));
});
This would output each actor added as an object to the console
Attachment 15761
Now, I updated my code with your function, and works so thank you. I was a bit lost as to how to load the stored objects back into the table, but the for loop makes sense now that I see how its done.
One part I have a question on.... I added the onclick='deleteActor(this)' to the <a> and it deletes it from the table, but not from storage. So with that, do I have to create another function to delete the row in storage, as I deleted it from the table? Or can I just do it in the deleteActor() function.
Here is my attempt to figure out how to delete from local storage, as well as the table. Now it deletes already from the table just fine. But when I refresh the page it's still there. Not sure, but I think i have to get the key of the local storage I want to delete, and somehow link it with the row I've selected to delete?
Code:
var deleteActor = function(e) {
// unbinds the delete actor button click, so you can delete multiple rows
$("#deleteActor").unbind();
$("#deleteActor").on('click', function(event) {
event.preventDefault();
var lsRow = JSON.parse(localStorage["lsActor"]);
for (var i=0; i < lsRow.length; i++) {
if (lsRow[i]) {
lsRow.removeItem(lsRow);
break;
}
}
var row = e.parentNode.parentNode;
row.parentNode.removeChild(row);
clearForm(actorForm);
actorState("new");
});
};
var addActor = function() {
// Assigning Form values to variables
var firstN = $("#fName").val();
var lastN = $("#lName").val();
var birthdate = $("#birthDate").val();
var gender = $("[name='gender']:checked").val();
var action = $("#action").prop('checked');
var comedy = $("#comedy").prop('checked');
var drama = $("#drama").prop('checked');
var sciencefiction = $("#sciencefiction").prop('checked');
var horror =$("#horror").prop('checked');
var suspense = $("#suspense").prop('checked');
// creates newActor variable that contains an object for each input value
var newActor = {fName: firstN, lName: lastN, birthDate: birthdate, gender: gender, action: action, comedy: comedy, drama: drama, suspense: suspense, sciencefiction: sciencefiction, horror: horror}
// Creates a table of links for each added actor with an id based from # of actors
$("#actorsTable").append("<tr><td><a href='' onclick='deleteActor(this); updateActor(this);' class='update' data-idx='" + actors.length + "'>" + newActor.fName + " " + newActor.lName + "</a></td></tr> ");
// pushes newActor into the end of actors array
actors.push(newActor);
alert(JSON.stringify(newActor));
$(".update").off("click");
$(".update").on("click", selectActor);
console.log(actors);
localStorage.setItem("lsActor", JSON.stringify(actors));
};
var addActorsOnLoad = function () {
var json_Actors = localStorage.getItem("lsActor");
if (json_Actors) {
actors = JSON.parse(json_Actors);
for (var i = 0; i < actors.length; i++) {
// Creates a table of links for each added actor with an id based from # of actors
$("#actorsTable").append("<tr><td><a href='' onclick='deleteActor(this); updateActor(this);' class='update' data-idx='" + i + "'>" + actors[i].fName + " " + actors[i].lName + "</a></td></tr> ");
}
$(".update").off("click");
$(".update").on("click", selectActor);
}
};
$(document).ready(function() {
addActorsOnLoad();
$( "#birthDate" ).datepicker();
actorState ("new");
var newActors2 = localStorage.getItem("lsActor")
console.log('newActors2', JSON.parse(newActors2));
$("#addNewActor").click(function() {
if(validateForm()==true)
{
addActor();
clearForm(actorForm);
}
});
$("#updateActor").click(function() {
if(validateForm()==true)
{
updateActor();
clearForm(actorForm);
}
});
$("#deleteActor").click(function() {
deleteActor();
localStorage.removeItem(actors[i]);
});
$("#clearButton").click(function () {
clearForm(actorForm);
});
});
Sorry for the wall of text, and thanks for your help again!
Codepen of the whole code