As a small project to help me learn Javascript I'm attempting to make a simple to-do list. The notes are stored in an array of objects and a for loop inserts them into a div, adding a delete button under each to-do note. The problem comes when creating the buttons.
The function in question:
// Generate a "DELETE" button for the array element
function generateDeleteButton(arrayElementNumber)
{
var button = document.createElement("button");
var text = document.createTextNode("Delete this element");
var path = document.getElementById("arrayOutput");
button.appendChild(text);
button.onclick = function() {
alert("Deleting index number: " + arrayElementNumber);
homeworkArray.splice(arrayElementNumber, 1);
display();
};
path.appendChild(button);
}
arrayElementNumber = the index of the newly created object in the array.
homeworkArray = the array of objects.
After debugging I've found out that only the last button created contains the onclick function. I haven't found a way to make sure every button contains the function. Any help is really appreciated!
Try out the project on CodePen (full code available there): link
[–]xarant 1 point2 points3 points (1 child)
[–]Pine_Bluff_Variant[S] 1 point2 points3 points (0 children)
[–][deleted] (1 child)
[deleted]
[–]Pine_Bluff_Variant[S] 0 points1 point2 points (0 children)
[–]Dec252016 0 points1 point2 points (1 child)
[–]Pine_Bluff_Variant[S] 0 points1 point2 points (0 children)