Reputation: 25
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>
Upvotes: 0
Views: 1173
Reputation: 220
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>
Upvotes: 0
Reputation: 177694
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>
Upvotes: 1