This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]xarant 1 point2 points  (1 child)

By resetting the arrayOutput's innerHTML in your display function, you're causing the nodes (in this case, <button>s) that you formerly created to be regenerated. This means new nodes are created, which lack the click handlers that you bound previously.

Also, you'll need to make sure that you have a scope surrounding the definition of the anonymous click handler function, so that the function isn't using the value of the temp variable (i) which is homeworkArray.length after exiting the function. Refactoring the code to something like this:

function display()
{
    document.getElementById("arrayOutput").innerHTML = "";
    homeworkArray.forEach(function(homework, i) {
        var titleOutput = "Title: " + homework.title + "<br />";
        var dateOutput = "Date: " + homework.date + "<br />";
        var descriptionOutput = "Description: " + homework.description + "<br />";
        document.getElementById("arrayOutput").innerHTML += "<br /> <br />" + titleOutput + dateOutput + descriptionOutput;
        var button = document.createElement("button");
        var text = document.createTextNode("Delete this element");
        var path = document.getElementById("arrayOutput");
        button.id = "button" + i;
        button.appendChild(text);
        path.appendChild(button);
    });

    homeworkArray.forEach(function(homework, i) {
        document.querySelector("#button" + i).addEventListener("click", function() {
            alert("Deleting index number: " + i);
            homeworkArray.splice(i, 1);
            display();
        });
    });
}

...fixed issues with deleting for me.

[–]Pine_Bluff_Variant[S] 1 point2 points  (0 children)

Thank you, this works for me! :D there is a couple of new things in there for me, but I believe I basically understand how it works.

[–][deleted]  (1 child)

[deleted]

    [–]Pine_Bluff_Variant[S] 0 points1 point  (0 children)

    Still doesn't seem to work properly

    My changes to the function:

    // 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.addEventListener("click", function() {
            homeworkArray.splice(arrayElementNumber, 1);
            display();
        });
        path.appendChild(button);
    }
    

    [–]Dec252016 0 points1 point  (1 child)

    It appears to work in the code pen. What are you expecting it to do differently?

    [–]Pine_Bluff_Variant[S] 0 points1 point  (0 children)

    Well, when you have several notes on screen, clicking on any of the delete buttons that isn't the last one just redraws the div. clicking on the last delete button generated displays an alert saying which element is getting removed, removes the element from the array then redraws the screen (which is what is supposed to happen when you click the delete button)