0

I'm quite new to Javascript and don't know where to start for this one. I have a list of checkboxes. I want to achieve when a checkbox is checked, the checkbox is removed from the list. I found some different solutions but they all have a button you need to press to remove the checkbox and i just want the checkbox removed when it's checked.

This is how my HTML looks like:

<ul class="list-group">
                    <li class="list-group-item">
                        <div class="checkbox">
                            <label>
                                <input type="checkbox"> Item 1
                            </label>
                        </div>
                    </li>
                    <li class="list-group-item">
                        <div class="checkbox">
                            <label>
                                <input type="checkbox"> Item 2
                            </label>
                        </div>
                    </li>
                </ul>
3
  • 1
    $(this).remove?? Commented Dec 7, 2017 at 8:56
  • 1. Find the type of element you are interested $('thatelement') 2. Attach .change method to that 3. Write function that you are expecting to happen when you do change that element (this).remove() or maybe (this).hide if you want just to hide it Commented Dec 7, 2017 at 8:57
  • Thanks for your help! i'll check this out :) Commented Dec 7, 2017 at 9:01

2 Answers 2

1

Depending on how precise you need to be, here is one that will remove the parent LI when clicked

You can add if (this.checked) { ... } if you only want to remove when checked

$(function() {
  $("input[type=checkbox]").on("click",function() {
    $(this).closest("li").remove(); // $(this).remove(); to remove the checkbox only
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<ul class="list-group">
  <li class="list-group-item">
    <div class="checkbox">
      <label><input type="checkbox"> Item 1</label>
    </div>
  </li>
  <li class="list-group-item">
    <div class="checkbox">
      <label><input type="checkbox"> Item 2</label>
    </div>
  </li>
</ul>

Sign up to request clarification or add additional context in comments.

Comments

0

See this code , this is removing current checkbox while you are clicking on checkbo

Code Here

$( '.checkbox input[type="checkbox"]' ).change( function(){
  $(this).remove();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="list-group">
                    <li class="list-group-item">
                        <div class="checkbox">
                            <label>
                                <input type="checkbox" class="checkbox">
                            </label>
                        </div>
                    </li>
                    <li class="list-group-item">
                        <div class="checkbox">
                            <label>
                                <input type="checkbox" class="checkbox">
                            </label>
                        </div>
                    </li>
                </ul>

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.