0

I am trying to add an element when a checkbox is check and remove that same element when a user unchecks the box.

Here is my code:

$(document).ready(function() {

      var increment = 0;
      var artist_to_compare = new Set();

      $(document).on("change", ".checkbox", function() {
          increment += this.checked ? 1 : -1;

          if (!this.checked) {

              // // delete the artist from the list of comparison
              $(element_to_delete).remove();
              artist_to_compare.delete(this.name);
              var element_to_delete = "." + this.name;
              console.log(element_to_delete);

          } else {

              // Add the artist to the list of comparison
              artist_to_compare.add(this.name);
              var element_to_add = '<p class="' + this.name + '">' + this.name + '</p>';
              console.log(element_to_add);
              $(".artistToCompare").after(element_to_add);

          }

          if (increment >= 2) {
              console.log(artist_to_compare);
              // enable the compare button

          }
      });
  });

I am able to correctly add the element but I cannot remove it. Please help!

4
  • 1
    you need to restructure your code. $(element_to_delete).remove(); is declared before you define element_to_delete. just move this var element_to_delete = "." + this.name; to the top of the if statement. Commented Jun 29, 2017 at 4:54
  • what values does element_to_delete have? Commented Jun 29, 2017 at 4:54
  • Have you tried remove()? Commented Jun 29, 2017 at 4:54
  • $("."+this.name).remove() in place of $(element_to_delete).remove() Commented Jun 29, 2017 at 4:55

2 Answers 2

2

You have written $(element_to_delete).remove(); before defining element_to_delete

artist_to_compare.delete(this.name);
  var element_to_delete = "." + this.name;
  console.log(element_to_delete);
$(element_to_delete).remove();
Sign up to request clarification or add additional context in comments.

Comments

0

Yes Rahul is right. You are using remove() function to delete element_to_delete before defining it. Checkout following code.

$(document).ready(function() {

    var increment = 0;
    var artist_to_compare = new Set();

    $(document).on("change", ".checkbox", function() {
        increment += this.checked ? 1 : -1;

        if (!this.checked) {

            // delete the artist from the list of comparison
            var element_to_delete = "." + this.name;
            console.log(element_to_delete);
            $(element_to_delete).remove();
            artist_to_compare.delete(this.name);

         } else {

            // Add the artist to the list of comparison
            artist_to_compare.add(this.name);
            var element_to_add = '<p class="' + this.name + '">' + this.name + '</p>';
            console.log(element_to_add);
            $(".artistToCompare").after(element_to_add);

        }

        if (increment >= 2) {
            console.log(artist_to_compare);
            // enable the compare button

        }
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.