How to fix "toggle" e "classList" on JavaScript

I have a list to add a class, but just items after add by input works with toggle. The items in the code don’t work.

I wonder if is something related to "this" property too.

Link to CodePen.

https://codepen.io/kennedyrmenezes/pen/BaQRXMq

li.addEventListener("click", function() {
    var finished = this.classList.toggle("done");
    var removeButton = document.createElement("button");
    removeButton.classList.add("deleteButton");

    if (finished) {
        removeButton.appendChild(document.createTextNode("remove"));
        removeButton.classList = "deleteButton";
        li.appendChild(removeButton);

        removeButton.addEventListener("click", function() {
            this.parentElement.remove();
        });
    } else {
        this.getElementsByClassName("deleteButton")[0].remove();
    }
})

48 thoughts on “How to fix "toggle" e "classList" on JavaScript”

  1. I was wondering if you ever thought of changing
    the structure of your blog? Its very well written; I love what youve got to say.
    But maybe you could a little more in the way of content so people could connect with it better.

    Youve got an awful lot of text for only having one or two images.

    Maybe you could space it out better? http://antiibioticsland.com/Ampicillin.htm

    Reply

Leave a Comment