I am currently trying to delete entries from my local storage list (array) with delete buttons next to each line. I made the function to delete it, and also the function that adds the button / each line in general. It looks like this:
function printData() {
let output,
i;
let element;
output = "";
for(i=0; i<commentsData.length; i++) {
// adding the delete button to each entry:
element = "<em id='i'>"+commentsData[i].date+"</em> -- " +
commentsData[i].contents + "<button id='delete' onclick='deleteEntry()'>Delete</button>" + "<br>";
output += element;
}
$("#comments").html(output);
}
And this is my delete function:
function deleteEntry() {
alert("Test");
if (commentsData.length > 0) {
commentsData.splice(commentsData.indexOf($(this)),1);
}
printData();
}
I also tried calling it in my init function:
function init() {
loadData();
printData();
$("#delete").click(deleteEntry);
};
But currently it is not working when i click the buttons. What could be the reason for that? I am not yet sure if my deleteEntry is working right now, but my problem is that the clicking of the buttons does not work. You dont need to fix my deleteEntry method - i will do that myself, i just need your help to get my buttons to work please, i would be very greatful! :)
<emelement for emphasis is OK but perhaps better to style using CSS.deleteEntryhas issues as you note, keeping the display and array or whatevercomments.Datais in sync with each other will be a part of your final solution.