I am trying to delete checked checkboxes in jQuery, but when i want to get state of checkbox i get error message.
jsfiddle
<div class="control-group">
<label class="checkbox"><input value="0" type="checkbox">Message 0</label>
<label class="checkbox"><input value="1" type="checkbox">Message 1</label>
<label class="checkbox"><input value="2" type="checkbox">Message 2</label>
<label class="checkbox"><input value="3" type="checkbox">Message 3</label>
<label class="checkbox"><input value="4" type="checkbox">Message 4</label>
<label class="checkbox"><input value="5" type="checkbox">Message 5</label>
<label class="checkbox"><input value="6" type="checkbox">Message 6</label>
<label class="checkbox"><input value="7" type="checkbox">Message 7</label>
<label class="checkbox"><input value="8" type="checkbox">Message 8</label>
</div>
<button class="btn" type="button" id="deleteAcc">Delete</button>
My jQuery code is:
$("#deleteAcc").on("click",function(){
$(".control-group label.checkbox").each(function(){
if (this.children(":first").is(':checked')) {
this.remove();
}
});
});
I am trying to delete checked checkboxes in jQuery, but when i want to get state of checkbox i get error message.
jsfiddle
<div class="control-group">
<label class="checkbox"><input value="0" type="checkbox">Message 0</label>
<label class="checkbox"><input value="1" type="checkbox">Message 1</label>
<label class="checkbox"><input value="2" type="checkbox">Message 2</label>
<label class="checkbox"><input value="3" type="checkbox">Message 3</label>
<label class="checkbox"><input value="4" type="checkbox">Message 4</label>
<label class="checkbox"><input value="5" type="checkbox">Message 5</label>
<label class="checkbox"><input value="6" type="checkbox">Message 6</label>
<label class="checkbox"><input value="7" type="checkbox">Message 7</label>
<label class="checkbox"><input value="8" type="checkbox">Message 8</label>
</div>
<button class="btn" type="button" id="deleteAcc">Delete</button>
My jQuery code is:
$("#deleteAcc").on("click",function(){
$(".control-group label.checkbox").each(function(){
if (this.children(":first").is(':checked')) {
this.remove();
}
});
});
Share
Improve this question
edited Nov 4, 2014 at 9:17
Ionică Bizău
113k93 gold badges307 silver badges487 bronze badges
asked Nov 4, 2014 at 8:52
MattMatt
8,94235 gold badges131 silver badges242 bronze badges
1
-
You could use
if (this.children[0].checked) this.parentNode.removeChild(this)
. – RobG Commented Nov 4, 2014 at 9:52
4 Answers
Reset to default 8You don't need each
method. Just select the checked inputs and remove the parents (the label elements containing the checkboxes):
$("#deleteAcc").on("click", function() {
$(".checkbox input:checked").parent().remove();
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="control-group">
<label class="checkbox">
<input value="0" type="checkbox">Message 0</label>
<label class="checkbox">
<input value="1" type="checkbox">Message 1</label>
<label class="checkbox">
<input value="2" type="checkbox">Message 2</label>
<label class="checkbox">
<input value="3" type="checkbox">Message 3</label>
<label class="checkbox">
<input value="4" type="checkbox">Message 4</label>
<label class="checkbox">
<input value="5" type="checkbox">Message 5</label>
<label class="checkbox">
<input value="6" type="checkbox">Message 6</label>
<label class="checkbox">
<input value="7" type="checkbox">Message 7</label>
<label class="checkbox">
<input value="8" type="checkbox">Message 8</label>
</div>
<button class="btn" type="button" id="deleteAcc">Delete</button>
The actual issue is that the this
context in the $.each
closure is a reference to the DOM node itself. One way you can fix this by wrapping this
in $
:
$("#deleteAcc").on("click",function(){
$(".control-group label.checkbox").each(function(){
if ($(this).children(":first").is(':checked')) {
$(this).remove();
}
});
});
Try this http://jsfiddle/zp3vwh1j/2/
$("#deleteAcc").on("click",function(){
$("input:checkbox").each(function() {
if ($(this).is(":checked")) {
$(this).parent().remove();
}
});
});
$().ready(function () {
$('body').on('click', '#deletebtn', function () {
$("#example1 tr").each(function () {
var rowSelector = $(this);
if (rowSelector.find("input[type='checkbox']").prop('checked'))
{
//THE MARKUP SHOWING THE ID IS NOT AVAILABLE
//POST A TABLE ENTRY TO CLEAR UP
var id = rowSelector.find('td').first().next().html();
var sendObj = {Id : id};
//USE JSON OBJECT
$.ajax({
url : "/page.aspx/deleteRecord",//CHANGE TO YOUR URL
dataType: "json",
data: sendObj,
type: "POST",
success: function () {
alert("entry deleted");
}
});
rowSelector.remove();
}
});
});
});