1

Got this function to extend an array (groups) by checking a checkbox:

$("input[type=checkbox]").click(function() {
     if($(this).is(":checked")){
         $(this).parent().data("groups").push("my");
          console.log($(this).parent().data("groups"));
      }
      else{
      //   $(this).parent().data("groups").pop();
      console.log($(this).parent().data("groups"));
       }

});

    <div class="item white" data-kpi="GDI" data-groups='["all", "numbers", "green", "square"]'>
<input type="checkbox" class="ps" name="add" value="my" />
</div>

I want to be able to remove the item again by unchecking the checkbox (there might be more than one 'my' group-items).

2
  • You can do this if you add something to the array that uniquely identifies the checkbox. Or if you don't care which item is removed, just use $(this).parent().data("groups").pop() as you already have in your code. Or what's the problem with that? Commented Jul 11, 2014 at 8:35
  • If you did .data(this.id) or anything else that is unique, you could actually remove it, for how do you remove a certain my from an array of my's ? Commented Jul 11, 2014 at 8:37

1 Answer 1

1

You can use splice to remove an element from an array.

array = ['my', 'name', 'my'];
index = array.indexOf('my');
array.splice(index, 1);
Sign up to request clarification or add additional context in comments.

2 Comments

and the part for actually removing it?
I didn't understand the question. This actually removes the element

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.