check all check boxes - javascript

I’m having some problems with some JavaScript I created. When a item from check all is Unchecked it needs to remove the checked from the checked all box, also when the check all is checked I don’t want to to pull through the other items only the check all box.
check all, item 1, item 2
Remove item 1 and check all un-checks and all that gets pulled through is item. If check all is checked the item 1 and item 2 are checked but then not pulled through.
// all "check all" checkboxes will have the class "checkall"
// and the id to match the location, e.g. wales, etc
$(".checkall").on('change', function() {
// all normal checkboxes will have a class "chk_xxxxxx"
// where "xxxx" is the name of the location, e.g. "chk_wales"
// get the class name from the id of the "check all" box
var checkboxesClass = '.chk_' + $(this).attr("id");
// now get all boxes to check
var boxesToCheck = $(checkboxesClass);
// check all the boxes
boxesToCheck.prop('checked', this.checked);
});
// display what the user has chosen
$("input[type='checkbox']").change(function() {
$("#checked").empty();
$("input[type='checkbox']:checked").each(function() {
// see if it is a "check all" box
if ($(this).attr("class") === "checkall") {
// Display the id of the "check all" box
$("#checked").html($("#checked").html() + '<h3>' + $(this).attr("id").toUpperCase() + "</h3>");
} else {
// display the label text for all normal checkboxes
$("#checked").html($("#checked").html() + $(this).next().text() + "<br>");
}
});
});
// With some more work you could make the Check All headings show when only one or two checkboxes were checked. It depends how you need it to work.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Test</title>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" id="wales" class="checkall" value="1">
<label for="wales">Check All</label>
<input type="checkbox" id="checkItem1" value="2" class="chk_wales">
<label for="checkItem1">Item 1</label>
<input type="checkbox" id="checkItem2" value="2" class="chk_wales">
<label for="checkItem2">Item 2</label>
<input type="checkbox" id="checkItem3" value="2" class="chk_wales">
<label for="checkItem3">Item 3</label>
<hr />
<input type="checkbox" id="west" class="checkall" value="3">
<label for="west">Check All</label>
<input type="checkbox" id="checkItem4" value="4" class="chk_west">
<label for="checkItem4">Item 1</label>
<input type="checkbox" id="checkItem5" value="4" class="chk_west">
<label for="checkItem5">Item 2</label>
<input type="checkbox" id="checkItem6" value="4" class="chk_west">
<label for="checkItem6">Item 3</label>
<hr />
<input type="checkbox" id="east" class="checkall" value="5">
<label for="east">Check All</label>
<input type="checkbox" id="checkItem7" value="6" class="chk_east">
<label for="checkItem7">Item 1</label>
<input type="checkbox" id="checkItem8" value="6" class="chk_east">
<label for="checkItem8">Item 2</label>
<input type="checkbox" id="checkItem9" value="6" class="chk_east">
<label for="checkItem9">Item 3</label>
<p>You have selected:</p>
<div id="checked">
</div>
</body>
</html>

Not sure what you mean by 'pulled through', but unchecking the check-all can be achieved through:
if (!$currentCheckbox.hasClass('checkall')) {
var $previousCheckAll = $currentCheckbox.prevAll('.checkall');
if ($previousCheckAll.length>0)
{
$previousCheckAll.first().prop('checked',false);
}
}
Just insert the code above beneath the line
$("#checked").empty();
of your JSbin.

This code should work if im not mistaken what you are trying to say. I have done for first set of checkboxes. You can do it for next two:
$("#check1 input[type=checkbox]").on("change", function() {
if($("#check1 input[type=checkbox]:checked").length == $("#check1 input[type=checkbox]").length) {
$("#wales").prop("checked",true);
}
else {
$("#wales").prop("checked",false);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" id="wales" class="checkall" value="1">
<label for="wales">Check All</label>
<section id="check1">
<input type="checkbox" id="checkItem1" value="2" class="chk_wales">
<label for="checkItem1">Item 1</label>
<input type="checkbox" id="checkItem2" value="2" class="chk_wales">
<label for="checkItem2">Item 2</label>
<input type="checkbox" id="checkItem3" value="2" class="chk_wales">
<label for="checkItem3">Item 3</label>
</section>
UPDATE
This will fulfill your requirement. Remove your $("input[type='checkbox']").change(function() code and use the updated one
$("#check1 input[type=checkbox]").on("change", function()
{
$("#checked").empty();
if($("#check1 input[type=checkbox]:checked").length == $("#check1 input[type=checkbox]").length)
{
$("#wales").prop("checked", true);
// Display the id of the "check all" box
$("#checked").html($("#checked").html() + "<h3>Wales</h3>");
}
else
{
$("#wales").prop("checked", false);
$("#check1 input[type=checkbox]:checked").each(function()
{
$("#checked").html($("#checked").html() + $(this).next().text() + "<br>");
});
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" id="wales" class="checkall" value="1">
<label for="wales">Check All</label>
<section id="check1">
<input type="checkbox" id="checkItem1" value="2" class="chk_wales">
<label for="checkItem1">Item 1</label>
<input type="checkbox" id="checkItem2" value="2" class="chk_wales">
<label for="checkItem2">Item 2</label>
<input type="checkbox" id="checkItem3" value="2" class="chk_wales">
<label for="checkItem3">Item 3</label>
</section>
<hr/>
<div id="checked">
</div>
Another Update
$(".checkall").on('change', function()
{
// all normal checkboxes will have a class "chk_xxxxxx"
// where "xxxx" is the name of the location, e.g. "chk_wales"
// get the class name from the id of the "check all" box
var checkboxesClass = '.chk_' + $(this).attr("id");
// now get all boxes to check
var boxesToCheck = $(checkboxesClass);
// check all the boxes
boxesToCheck.prop('checked', this.checked);
});
$("#check1 input[type=checkbox], #wales").on("change", function()
{
checkBoxes();
});
function checkBoxes()
{
$("#checked").empty();
if($("#check1 input[type=checkbox]:checked").length == $("#check1 input[type=checkbox]").length)
{
$("#wales").prop("checked", true);
// Display the id of the "check all" box
$("#checked").html($("#checked").html() + "<h3>Wales</h3>");
}
else
{
$("#wales").prop("checked", false);
$("#check1 input[type=checkbox]:checked").each(function()
{
$("#checked").html($("#checked").html() + $(this).next().text() + "<br>");
});
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" id="wales" class="checkall" value="1">
<label for="wales">Check All</label>
<section id="check1">
<input type="checkbox" id="checkItem1" value="2" class="chk_wales">
<label for="checkItem1">Item 1</label>
<input type="checkbox" id="checkItem2" value="2" class="chk_wales">
<label for="checkItem2">Item 2</label>
<input type="checkbox" id="checkItem3" value="2" class="chk_wales">
<label for="checkItem3">Item 3</label>
</section>
<hr/>
<div id="checked">
</div>

Related

using checkboxes for multiple instances

I have this script. Everything is working except I need to make it work with multi-able items. The first set of work perfectly, but I can seam to replicate it on the second and third set of check boxes.
$(".checkall").on('change', function()
{
// all normal checkboxes will have a class "chk_xxxxxx"
// where "xxxx" is the name of the location, e.g. "chk_wales"
// get the class name from the id of the "check all" box
var checkboxesClass = '.chk_' + $(this).attr("id");
// now get all boxes to check
var boxesToCheck = $(checkboxesClass);
// check all the boxes
boxesToCheck.prop('checked', this.checked);
});
$("#check1 input[type=checkbox], #wales").on("change", function()
{
checkBoxes();
});
function checkBoxes()
{
$("#checked").empty();
if($("#check1 input[type=checkbox]:checked").length == $("#check1 input[type=checkbox]").length)
{
$("#wales").prop("checked", true);
// Display the id of the "check all" box
$("#checked").html($("#checked").html() + "<h3>Wales</h3>");
}
else
{
$("#wales").prop("checked", false);
$("#check1 input[type=checkbox]:checked").each(function()
{
$("#checked").html($("#checked").html() + $(this).next().text() + "<br>");
});
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Test</title>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- <input type="checkbox" id="wales" class="checkall" value="1">
<label for="wales" >Check All</label>
<input type="checkbox" id="checkItem1" value="2" class="chk_wales">
<label for="checkItem1" >Item 1</label>
<input type="checkbox" id="checkItem2" value="2" class="chk_wales">
<label for="checkItem2" >Item 2</label>
<input type="checkbox" id="checkItem3" value="2" class="chk_wales">
<label for="checkItem3" >Item 3</label>-->
<hr />
<input type="checkbox" id="wales" class="checkall" value="1">
<label for="wales">Check All</label>
<section id="check1">
<input type="checkbox" id="checkItem1" value="2" class="chk_wales">
<label for="checkItem1">Item 1</label>
<input type="checkbox" id="checkItem2" value="2" class="chk_wales">
<label for="checkItem2">Item 2</label>
<input type="checkbox" id="checkItem3" value="2" class="chk_wales">
<label for="checkItem3">Item 3</label>
</section>
<hr />
<input type="checkbox" id="west" class="checkall" value="3">
<label for="west">Check All</label>
<section id="check2">
<input type="checkbox" id="checkItem4" value="4" class="chk_west">
<label for="checkItem4">Item 1</label>
<input type="checkbox" id="checkItem5" value="4" class="chk_west">
<label for="checkItem5">Item 2</label>
<input type="checkbox" id="checkItem6" value="4" class="chk_west">
<label for="checkItem6">Item 3</label>
</section>
<hr />
<input type="checkbox" id="east" class="checkall" value="5">
<label for="east">Check All</label>
<section id="check3">
<input type="checkbox" id="checkItem7" value="6" class="chk_east">
<label for="checkItem7">Item 1</label>
<input type="checkbox" id="checkItem8" value="6" class="chk_east">
<label for="checkItem8">Item 2</label>
<input type="checkbox" id="checkItem9" value="6" class="chk_east">
<label for="checkItem9">Item 3</label>
</section>
<p>You have selected:</p>
<div id="checked">
</div>
</body>
</html>
Please can anyone help?
Many thanks in advance.
remove checkBoxes() method and replace the last event handlers as (check this fiddle)
//put the event handler directly on the checkboxes inside the section
$( "section input[type='checkbox']" ).on("change", function()
{
console.log( $( this ) );
//get the handle to the parent section
var $parentSection = $( this ).parent();
//compare the number of checked checkboxes with total one
if( $parentSection.find( "input[type='checkbox']:checked" ).length == $parentSection.find( "input[type=checkbox]" ).length )
{
$parentSection.prev().prev().prop("checked", true);
//write checkall's textbox id
$("#checked").append( "<h3>" + $parentSection.prev().prev().attr( "id" ) + "</h3>");
}
else
{
$parentSection.prev().prev().prop("checked", false);
//write each and every checked box's text
$parentSection.find("input[type='checkbox']:checked").each(function()
{
$("#checked").html($("#checked").html() + $(this).next().text() + "<br>");
});
}
});
Following code snippet should fulfill your requirements. Hope this will help you.
var checkedDiv = $("#checked");
$('input[type=checkbox]').on('change', function() {
if (this.className == 'checkall') {
var section = $(this).nextAll('section:first');
section.find('input[type=checkbox]').prop('checked', this.checked);
} else {
var parentSection = $(this).parent();
var checkall = parentSection.prevAll(".checkall:first")
var isEqual = parentSection.find("input[type=checkbox]:checked").length == parentSection.find("input[type=checkbox]").length;
checkall.prop("checked", isEqual);
}
checkedDiv.html('');
$('.checkall').each(function() {
if (this.checked) {
checkedDiv.append('<h3>' + this.id + '</h3><br/>');
} else {
var section = $(this).nextAll('section:first');
section.find("input[type=checkbox]:checked").each(function() {
checkedDiv.append($(this).next().text() + "<br>");
});
}
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" id="wales" class="checkall" value="1">
<label for="wales">Check All</label>
<section id="check1">
<input type="checkbox" id="checkItem1" value="2" class="chk_wales">
<label for="checkItem1">Item 1</label>
<input type="checkbox" id="checkItem2" value="2" class="chk_wales">
<label for="checkItem2">Item 2</label>
<input type="checkbox" id="checkItem3" value="2" class="chk_wales">
<label for="checkItem3">Item 3</label>
</section>
<hr />
<input type="checkbox" id="west" class="checkall" value="3">
<label for="west">Check All</label>
<section id="check2">
<input type="checkbox" id="checkItem4" value="4" class="chk_west">
<label for="checkItem4">Item 1</label>
<input type="checkbox" id="checkItem5" value="4" class="chk_west">
<label for="checkItem5">Item 2</label>
<input type="checkbox" id="checkItem6" value="4" class="chk_west">
<label for="checkItem6">Item 3</label>
</section>
<hr />
<input type="checkbox" id="east" class="checkall" value="5">
<label for="east">Check All</label>
<section id="check3">
<input type="checkbox" id="checkItem7" value="6" class="chk_east">
<label for="checkItem7">Item 1</label>
<input type="checkbox" id="checkItem8" value="6" class="chk_east">
<label for="checkItem8">Item 2</label>
<input type="checkbox" id="checkItem9" value="6" class="chk_east">
<label for="checkItem9">Item 3</label>
</section>
<p>You have selected:</p>
<div id="checked"></div>

How to show set of Checkbox based on a previous checkbox

I have a form with a list of checkbox on it. When a user selects "Laboratory", I want the second list of checkbox to be shown. I dont want the second list of checkboxes to be shown until they have clicked on the "Laboratory" checkbox.
How do I accomplish this in Javascript or jQuery?
Here's the php code for first list of checkbox
<?php
$tsql = "select medTestName from medtest";
$tstmt = $con->prepare($tsql);
$tstmt->execute();
$tstmt->bind_result($mtn);
$tstmt->store_result();
while ($tstmt->fetch()){
$d1= '<input type="checkbox" name="test[]"
value="'.$mtn.'">'.$mtn.'<br>';
echo $d1;
}
?>
Here's the image of database which is the first list of checkbox
Here's the image or the UI of the first list of checkbox
Here's my html code for the second list of checkbox
<div><input type="checkbox" name="topic" value="1"><span>Complete Blood Count</span></div>
<div><input type="checkbox" name="topic" value="2"><span>Blood Typing</span></div>
<div><input type="checkbox" name="topic" value="3"><span>Urinalysis</span></div>
<div><input type="checkbox" name="topic" value="4"><span>RPR/TPHA</span></div>
<div><input type="checkbox" name="topic" value="5"><span>Hepatitis B screening</span></div>
<div><input type="checkbox" name="topic" value="6"><span>Fasting Blood Sugar</span></div>
<div><input type="checkbox" name="topic" value="7"><span>Creatinine</span></div>
<div><input type="checkbox" name="topic" value="8"><span>Total Cholesterol(Low Cholesterol, High Cholesterol)</span></div>
<div><input type="checkbox" name="topic" value="9"><span>Triglyceride</span></div>
<div><input type="checkbox" name="topic" value="10"><span>VLDL</span></div>
<div><input type="checkbox" name="topic" value="11"><span>Blood Uric Acid</span></div>
<div><input type="checkbox" name="topic" value="12"><span>Anti-HAV Igm Screening</span></div>
<div><input type="checkbox" name="topic" value="13"><span>Anti HBaAg</span></div>
<div><input type="checkbox" name="topic" value="14"><span>Drug & Alcohol Test</span></div>
<div><input type="checkbox" name="topic" value="15"><span>Stool Culture</span></div>
You can use like below,
Also you can make it dynamic using php
$(document).ready(function() {
//initially hide all the sub lists
$(".sublists").children().each(function() {
if ($(this).attr("data-sub-list") == "true") {
$(this).hide();
}
});
// when you click the checkbox show Radiology,Radiology or ect.
$("input[name=test]").click(function() {
var list = $(this).attr("data-list");
console.log(list);
if ($(this).prop('checked')) {
$("#" + list + "").show();
} else {
$("#" + list + "").hide();
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="col-xs-7">
<input type="checkbox" name="test" id="Radiology" value="Radiology" data-list="Radiology-list">Radiology
<input type="checkbox" name="test" id="Laboratory" value="Laboratory" data-list="Laboratory-list">Laboratory
</div>
<div class="sublists">
<div id="Laboratory-list" data-sub-list="true">
<h4>Laboratory list</h4>
<div class="col-xs-7">
<input type="checkbox" name="topic" value="Creatinine">Creatinine
<input type="checkbox" name="topic" value="Triglyceride">Triglyceride
<input type="checkbox" name="topic" value="VLDL">VLDL
</div>
</div>
<div id="Radiology-list" data-sub-list="true">
<h4>Radiology list</h4>
<div class="col-xs-7">
<input type="checkbox" name="topic" value="one">One
<input type="checkbox" name="topic" value="two">Two
</div>
</div>
</div>
In all cases, you have to generate it (in a hidden ul for example) and show it with jQuery like $("#mydiv").toggle();
Here is the jsfiddle : https://jsfiddle.net/6ufzban3/
You can use the JQuery .toggle() function to show/hide elements.
$(document).ready(function() {
$('input[type="checkbox"]').click(function() {
if ($(this).attr("value") == "lab") {
$(".labboxes").toggle();
}
});
});
.labboxes {
margin: 10px;
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox">Radiology
<input type="checkbox" value="lab">Laboratory
<div class="labboxes">
<div>
<input type="checkbox" name="topic"><span>Triglyceride</span>
</div>
<div>
<input type="checkbox" name="topic"><span>VLDL</span>
</div>
<div>
<input type="checkbox" name="topic"><span>Creatinine</span>
</div>
</div>
<?php
$tsql = "select medTestName from medtest";
$tstmt = $con->prepare($tsql);
$tstmt->execute();
$tstmt->bind_result($mtn);
$tstmt->store_result();
while ($tstmt->fetch()){
$d1= '<input type="checkbox" name="test[]" value="'.$mtn.'">'.$mtn.'<br>';
echo $d1;
}
?>

Naming html optionfields and checkboxes to use it properly in jquery

I'm trying to get a value in array and then display it. This works well for the one I have
html:<input id="email" type="email" name="email" required="required" />
jquery:
$('#email').on('keyup', updateStatus);
person[0] = $('#email').val();
but when I do this with optionfields or checkboxes, this doesn't work: I always get the value female in the optionfields and the value 1 in the checkboxes. How do I have to name them to get it working?
<input type="radio" id="gender" name="gender" value="Male">Male</input>
<input type="radio" id="gender" name="gender" value="Female">Female</input>
<label>
<input type="checkbox" id="chbx1" name="sportart" value="1" />1</label>
<label>
<input type="checkbox" id="chbx1" name="sportart" value="2" />2</label>
<label>
<input type="checkbox" id="chbx1" name="sportart" value="3" />3</label>
<label>
<input type="checkbox" id="chbx1" name="sportart" value="4" />4</label>
And the jquery:
$('#gender').on('blur', updateStatus);
$('#chbx1').on('blur', updateStatus);
person[3] = $('#gender').val();
person[4] = $('#chbx1').val();
This is my updateStatus function:
function updateStatus() {
var person = [];
person[0] = $('#email').val();
person[1] = $('#passwort').val();
person[2] = $('#passwortwd').val();
person[3] = $('#gender').val();
person[4] = $('#chbx1').val();
person[5] = $('#hobby').val();
$('#status').text(JSON.stringify(person));
}
HTML ID should be unique. Several items cannot have the same ID.
Selection by ID (#...) will always return one item.
For example, check this snipper out:
document.write("By name:" + $("[name='gender']").length + "<br/>");
document.write("By ID:" + $("#gender").length + "<br/>");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="radio" id="gender" name="gender" value="Male"/>Male
<input type="radio" id="gender" name="gender" value="Female"/>Female
<br/><br/>
You should assign event to all elements with name sportart this way:
$("input[name='sportart']").on('change', function() {
alert(this.value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="sportart" value="1" />1<br/>
<input type="checkbox" name="sportart" value="2" />2<br/>
<input type="checkbox" name="sportart" value="3" />3<br/>
<input type="checkbox" name="sportart" value="4" />4
You can always get the selected radio or checkbox value using the :checked selector:
var arr = $("input[name='sportart']:checked"); // for checkbox it is array
alert(arr.length); // count of selected items
alert(arr[0].value); // value of the first one
$("input[name='gender']:checked").val();
Try using change handler and use name attribute selector:
$('input[name=gender], input[name=sportart]').on('change', updateStatus);
person[3] = $('input[name=gender]:checked').val();
person[4] = $('input[name=sportart]:checked').val();
Example:
var pushStatus = function() {
var o = {};
o.gender = $('input[name=gender]:checked').val();
o.sportart = $('input[name=sportart]:checked').map(function() {
return this.value;
}).get().join(',');
$('#status').html(JSON.stringify(o));
};
$(function() {
$('input[name=gender], input[name=sportart]').on('change', pushStatus);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type="radio" name="gender" value="Male" />Male
<input type="radio" name="gender" value="Female" />Female
<label>
<input type="checkbox" name="sportart" value="1" />1</label>
<label>
<input type="checkbox" name="sportart" value="2" />2</label>
<label>
<input type="checkbox" name="sportart" value="3" />3</label>
<label>
<input type="checkbox" name="sportart" value="4" />4</label>
<div id="status"></div>

jQuery (Mobile): get all checked items (checkboxes)

I got trouble finding a solution to this one.
I have an amount of checkboxes, and I want to get how many of them are checked.
Each time a box get checked/unchecked, the value needs to update.
What I have so far:
http://jsfiddle.net/drhmorw5/3/
My code so far:
function selected() {
var i = 0;
$("#names").each(function () {
if ($(this).prop("checked") === true) {
i++;
$("#checked").text("Sum Checked: " + i);
}
})
};
$("#checked").text("Sum Checked: ");
$(function(){
$('#names input[type=checkbox]').change(function(){
$("#checked").text($('#names input[type=checkbox]:checked').length);
});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<div id="names">
<label>
<input type="checkbox" name="checkbox-0" class="check">Name 1</label>
<label>
<input type="checkbox" name="checkbox-0" class="check">Name 2</label>
<label>
<input type="checkbox" name="checkbox-0" class="check">Name 3</label>
<label>
<input type="checkbox" name="checkbox-0" class="check">Name 4</label>
<label>
<input type="checkbox" name="checkbox-0" class="check">Name 5</label>
<label>
<input type="checkbox" name="checkbox-0" class="check">Name 6</label>
</div>
<br>
<br>
Sum Checked: <span id="checked">0</span>

Delete checked checkboxes in jQuery

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();
}
});
});
You 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.com/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.net/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();
}
});
});
});

Categories

Resources