Select all checkboxes using Javascript or jQuery - javascript

I want to check/uncheck all check boxes of a particular row in a table if Select all Checkbox is selected for that particular row
Here is the html for a single row.
<tr>
<td class="left">
<label>Admin - Organization</label>
</td>
<td>
<div class="checkbox">
<label><asp:CheckBox cssclass="selectallAO" runat="server" /></label>
</div>
</td>
<td>
<div class="checkbox">
<label><asp:CheckBox cssclass="checkboxAO" ID="chkbxViewAOrganization" runat="server" /></label>
</div>
</td>
<td>
<div class="checkbox">
<label><asp:CheckBox cssclass="checkboxAO" ID="chkbxAddAOrganization" runat="server" /></label>
</div>
</td>
<td>
<div class="checkbox">
<label><asp:CheckBox cssclass="checkboxAO" ID="chkbxEditAOrganization" runat="server" /></label>
</div>
</td>
<td>
<div class="checkbox">
<label><asp:CheckBox cssclass="checkboxAO" ID="chkbxDeleteAOrganization" runat="server" /></label>
</div>
</td>
</tr>
There are 5 other rows like this for other pages as well. So, I want to check/uncheck all checkbox's for a particular row for which Select All was checked/unchecked.

UPDATE
Updated the jQuery function to make the master checkbox override child checkbox states.
How does something like this sound? I wasn't sure which classes were your checkboxes so you'll have to tweak this a bit:
HTML:
<div class="spacer"><input type="checkbox" class="selectallAO"> Select All</div>
<div class="spacer"><input type="checkbox" class="checkboxAO"> Regular</div>
<div class="spacer"><input type="checkbox" class="checkboxAO"> Regular</div>
CSS:
div.spacer {
display: block;
}
JS:
$('.selectallAO').click(function() {
this.checked ? $('.checkboxAO').prop('checked', true) : $('.checkboxAO').prop('checked', false);
});
jsFiddle

The .parents("form:first") selector ensures that it only applies to those elements that are in the same form as the inputs you want to change. If for some reason you want to change elements outside of the form then you will need to remove that.
$(function() {
$(".selectallAO").on("click", function() {
debugger;
if ($(this).prop("checked")) {
$(this).parents("form:first").children(".checkboxAO").prop("checked", true);
} else {
$(this).parents("form:first").children(".checkboxAO").prop("checked", false);
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<input type="checkbox" name="test" value="1" class="selectallAO">Select All</input>
<input type="checkbox" name="test" value="1" class="checkboxAO">1</input>
<input type="checkbox" name="test" value="2" class="checkboxAO">2</input>
<input type="checkbox" name="test" value="3" class="checkboxAO">3</input>
<input type="checkbox" name="test" value="4" class="checkboxAO">4</input>
</form>

Related

Finding data attribute and checkbox children

I hope you can see what I'm trying to do here.
I am trying to keep the same structure for the divs but clicking the select all in the top area only selects those 3 and the bottom select all only selects the bottom 3.
Obviously there are better ways to do this but I have stripped my code back to this basic example.
please help me rewrite this line:
$("div").find("[data-profile-id='"+profileID+"']").find(".score_alert_cb1").each(function() {
So i can use the profile-id data attribute to select the checkboxes that fall inside that div.
Thanks
$('.profile #select_all_cb').click(function() {
var profileID = $(this).closest('.profile').data('profile-id');
if($(this).is(":checked")) {
$("div").find("[data-profile-id='"+profileID+"']").find(".score_alert_cb1").each(function() {
$(this).prop('checked', true);
});
} else {
$("div").find("[data-profile-id='"+profileID+"']").find(".score_alert_cb1").each(function() { //Check all boxes
$(this).prop('checked', false);
});
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="profile" data-profile-id="1">
<div>
A lot of data before my checkbox
</div>
<div>
<input type="checkbox" id="select_all_cb">select all
</div>
<div>
<input type="checkbox" class="score_alert_cb1">
<input type="checkbox" class="score_alert_cb1">
<input type="checkbox" class="score_alert_cb1">
</div>
</div>
<br /><br />
<div class="profile" data-profile-id="2">
<div>
A lot of data before my checkbox
</div>
<div>
<input type="checkbox" id="select_all_cb">select all
</div>
<div>
<input type="checkbox" class="score_alert_cb1">
<input type="checkbox" class="score_alert_cb1">
<input type="checkbox" class="score_alert_cb1">
</div>
</div>
IDs are unique, so changed id="select_all_cb" to class="select_all_cb".
On click .select_all_cb, goes up and finds .profile parent, store the checkbox value as boolean in isChecked variable, and finds all .score_alert_cb1 checkboxes within the current .profile element.
$(".profile .select_all_cb").click(function() {
var profile = $(this).closest(".profile");
// var profileID = profile.data('profile-id');
var isChecked = $(this).is(":checked");
profile.find(".score_alert_cb1").prop("checked", isChecked);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="profile" data-profile-id="1">
<div>
A lot of data before my checkbox
</div>
<div>
<input type="checkbox" class="select_all_cb">select all
</div>
<div>
<input type="checkbox" class="score_alert_cb1">
<input type="checkbox" class="score_alert_cb1">
<input type="checkbox" class="score_alert_cb1">
</div>
</div>
<br /><br />
<div class="profile" data-profile-id="2">
<div>
A lot of data before my checkbox
</div>
<div>
<input type="checkbox" class="select_all_cb">select all
</div>
<div>
<input type="checkbox" class="score_alert_cb1">
<input type="checkbox" class="score_alert_cb1">
<input type="checkbox" class="score_alert_cb1">
</div>
</div>
What's wrong with
$("div").find("[data-profile-id='"+profileID+"']").find("[type=checkbox]")each(...}
?

jQuery traverse down multiple layers deep to set checked value

How do I use jQuery to traverse to this element deep in my table?
#adminreview > thead > tr > th:nth-child(1) > div > div > div.checkbox-container > div:nth-child(1) > input
and set the input's checked property to false?
I'm using this nice filter/sort jQuery plug in (Excel-like Bootstrap Table Sorting And Filtering Plugin) on my table. On load, I'm trying to only have one of the options checked, Pending, and will run the jQuery to hide the Approved & Denied rows separately.
On load, all statuses are currently checked by default:
Goal is to only have Pending checked on load:
Here is my table html. I know this has to do with traversing the DOM and setting the checked value for Select All, Approved, & Denied to false, but I'm not having much luck.
I assume I'd have to edit div:nth-child(1) (2) & (3) and use .prop('checked', false); Just not sure how to go that deep using jquery.
HTML (I didn't include the entire table on purpose):
<table id="adminreview" class="fbody">
<thead>
<tr>
<th class="apply-filter no-search">Status
<div class="dropdown-filter-dropdown">
<div class="dropdown-filter-content" style="display: none;">
<div class="dropdown-filter-sort">
<span class="a-to-z" data-column="0" data-index="0">A to Z</span>
</div>
<div class="dropdown-filter-sort">
<span class="z-to-a" data-column="0" data-index="0">Z to A</span>
</div>
<div class="checkbox-container">
<div class="dropdown-filter-item">
<input type="checkbox" value="Select All" checked="checked" class="dropdown-filter-menu-item select-all"
data-column="0" data-index="0" /> Select All</div>
<div class="dropdown-filter-item">
<input type="checkbox" value="Approved" checked="checked" class="dropdown-filter-menu-item item" data-column="0"
data-index="0" /> Approved</div>
<div class="dropdown-filter-item">
<input type="checkbox" value="Denied" checked="checked" class="dropdown-filter-menu-item item" data-column="0"
data-index="0" /> Denied</div>
<div class="dropdown-filter-item">
<input type="checkbox" value="Pending" checked="checked" class="dropdown-filter-menu-item item" data-column="0"
data-index="0" /> Pending</div>
</div>
</div>
</div></th>
<th class="apply-filter">Company
<div class="dropdown-filter-dropdown">
<div class="dropdown-filter-content" style="display: none;">
<div class="dropdown-filter-sort">
<span class="a-to-z" data-column="1" data-index="1">A to Z</span>
</div>
<div class="dropdown-filter-sort">
<span class="z-to-a" data-column="1" data-index="1">Z to A</span>
</div>
<div class="dropdown-filter-search">
<input type="text" class="dropdown-filter-menu-search form-control" data-column="1" data-index="1"
placeholder="Search" />
</div>
<div class="checkbox-container">
<div class="dropdown-filter-item">
<input type="checkbox" value="Select All" checked="checked" class="dropdown-filter-menu-item select-all"
data-column="1" data-index="1" /> Select All</div>
<div class="dropdown-filter-item">
<input type="checkbox" value="ACME Construction Company" checked="checked" class="dropdown-filter-menu-item item"
data-column="1" data-index="1" /> ACME Construction Company</div>
<div class="dropdown-filter-item">
<input type="checkbox" value="Joe's Concrete" checked="checked" class="dropdown-filter-menu-item item"
data-column="1" data-index="1" /> Joe's Concrete</div>
</div>
</div>
</div></th>
There are other columns with the same class that I don't want to touch:
Thanks in advance!
You've item class on every checkbox. So what you can is loop through all the nodes and set checked to false for all. And then. Set checked to true for the last one.
Like
$('.item').each(function() {
$( this ).prop('checked', false);
});
$('.item').last().prop('checked', true);

Only ONE button can be clicked per row

I'm trying to code that only ONE button can be selected per ROW. I tried too many this and this is the closest I did it.
I can selected and deselect it. But only one per row can be selected.
What my code does:
I can select any button and it will be added a class to it get red.
so now this button is selected. I can unselect it as well.
This is my code:
$('.btn').click(function(){
if ($(this).attr('data-selected') === 'true') {
$(this).attr('data-selected', 'false');
$(this).removeClass('selected');
}else {
$(this).attr('data-selected', 'true');
$(this).addClass('selected');
}
});
<style>
.selected {
color:red;
}
</style>
Some print to help:
You can do it like following. Just find the others .btn and remove .selected from them and set data-selected to false when you are selecting a .btn. Hope this will help you.
$('.btn').click(function () {
if ($(this).attr('data-selected') === 'true') {
$(this).attr('data-selected', 'false');
$(this).removeClass('selected');
} else {
$(this).closest('tr').find('.btn').not(this)
.removeClass('selected').attr('data-selected', 'false');
$(this).attr('data-selected', 'true');
$(this).addClass('selected');
}
});
.selected {
color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<td>1</td>
<td>2</td>
<td><input type="button" value="1.22" class="btn"/></td>
<td><input type="button" value="1.33" class="btn" /></td>
<td><input type="button" value="1.44" class="btn" /></td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td><input type="button" value="1.22" class="btn" /></td>
<td><input type="button" value="1.33" class="btn" /></td>
<td><input type="button" value="1.44" class="btn" /></td>
</tr>
</table>
Why not just make radio buttons that looks like buttons, if you give them the same name they will already have the functionality you are looking for built in.
http://jsfiddle.net/DIRTY_SMITH/YB8UW/616/
<ul class="donate-now">
<li>
<input type="radio" id="a25" name="amount" />
<label for="a25">$25</label>
</li>
<li>
<input type="radio" id="a50" name="amount" />
<label for="a50">$50</label>
</li>
<li>
<input type="radio" id="a75" name="amount" checked="checked" />
<label for="a75">$75</label>
</li>
<li>
<input type="radio" id="a100" name="amount" />
<label for="a100">$100</label>
</li>
</ul>
<br>
<ul class="donate-now">
<li>
<input type="radio" id="b25" name="amountb" />
<label for="b25">$25</label>
</li>
<li>
<input type="radio" id="b50" name="amountb" />
<label for="b50">$50</label>
</li>
<li>
<input type="radio" id="b75" name="amountb" checked="checked" />
<label for="b75">$75</label>
</li>
<li>
<input type="radio" id="b100" name="amountb" />
<label for="b100">$100</label>
</li>
</ul>
I made a working example for you which can be used for multiple rows: https://jsfiddle.net/448feo3j/
$('.button-default').click(function() {
$(this).parents('tr').find('.button-default').removeClass('button-clicked').attr('data-selected', false);
$(this).addClass('button-clicked').attr('data-selected', true);
});

Check box class based click function not working

I am trying to find out which check box is selected in a group of check boxes and read its value.
The following is the checkbox format
<div class="panel-body smoothscroll maxheight300 brand_select">
<div class="block-element">
<label>
<input type="checkbox" name="term" value="0" />
Armani </label>
</div>
<div class="block-element">
<label>
<input type="checkbox" name="term" value="1" />
Gucci </label>
</div>
<div class="block-element">
<label>
<input type="checkbox" name="term" value="2" />
Louis Vuitton </label>
</div>
</div>
I tried giving class names but it didn't work. I gave class names as class="brand_name" and tried to read through jquery click function
$('.brand_name').click(function () {
console.log("click worked")
});
But it didn't work. Can someone suggest me a way to do this.
You are applying the click event handler on div and not on checkboxes
$('.brand_select').on('click', ':checkbox', function() {
alert($(this).is(':checked'));
});
To get list of all checked values:
$('.brand_select').on('click', ':checkbox', function() {
var checkedEl = [];
$('.brand_name :checkbox').each(function () {
if ($(this).is(':checked')) {
checkedEl.push($(this).val());
}
});
console.log('Checked values');
console.log(checkedEl);
});
$('input[type="checkbox"]').on('click',function () {
if(this.checked){
alert($(this).val());
}else{
// .........
}
})
Add the class to input elements and change the name of the fields so that you can use them as array -
<input type="checkbox" name="term[]" value="2" class="brand_name"/>
Here is a possible solution:
$('input[name="term"]').on('change', function(){
alert($(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="panel-body smoothscroll maxheight300 brand_select">
<div class="block-element">
<label>
<input type="checkbox" name="term" value="0" />
Armani </label>
</div>
<div class="block-element">
<label>
<input type="checkbox" name="term" value="1" />
Gucci </label>
</div>
<div class="block-element">
<label>
<input type="checkbox" name="term" value="2" />
Louis Vuitton </label>
</div>
</div>

jQuery - Uncheck other checkboxes if a specific checkbox is selected by user

I would like to uncheck all the checkboxes that are presently selected if a specific checkbox is selected by the user.
Example:
<div>
<label for="foo">
<input type="checkbox" name="meh" id="foo" checked> foo
</label>
</div>
<div>
<label for="bar">
<input type="checkbox" name="meh" id="bar" checked> bar
</label>
</div>
<div>
<label for="foobar">
<input type="checkbox" name="meh" id="foobar"> foobar
</label>
</div>
<div>
<label for="barfoo">
<input type="checkbox" name="meh" id="barfoo" checked> barfoo
</label>
</div>
<div>
<label for="omgwtfbbq">
<input type="checkbox" name="meh" id="omgwtfbbq"> omgwtfbbq
</label>
</div>
If the user selects "omgwtfbbq" checkbox, I would like all the other boxes that might be checked to be unchecked and have the "omgwtfbbq" be the only one checked.
for the label instead of id I think you need for
<div>
<label for="foo">
<input type="checkbox" name="foo" id="foo" checked /> foo
</label>
</div>
<div>
<label for="bar">
<input type="checkbox" name="bar" id="bar" checked /> bar
</label>
</div>
<div>
<label for="foobar">
<input type="checkbox" name="foobar" id="foobar" /> foobar
</label>
</div>
<div>
<label for="barfoo">
<input type="checkbox" name="barfoo" id="barfoo" checked /> barfoo
</label>
</div>
<div>
<label for="omgwtfbbq">
<input type="checkbox" name="omgwtfbbq" id="omgwtfbbq" /> omgwtfbbq
</label>
</div>
then
var $others = $('input[type="checkbox"][name="meh"]').not('#omgwtfbbq')
$('#omgwtfbbq').change(function () {
if (this.checked) {
$others.prop('checked', false)
}
});
$others.change(function () {
if (this.checked) {
$('#omgwtfbbq').prop('checked', false)
}
})
Demo: Fiddle
Note: I'll add a common class to all the input elements which has to be affected by omgwtfbbq and change var $others = $('#foo, #bar, #foobar, #barfoo') to var $others = $('.myclassoninput')
Live demo (click).
$('#omgwtfbbq').click(function() {
$('input:checkbox').not(this).attr('checked', false);
});
Also note that you're re-using id's. Id's should only be used once in a document.
If you choose not to give each checkbox a sequential IDs so that you can use an array, here's a solution:
Place all your controls in a div, with an ID "checkgroup".
Then the JavaScript function goes:
function checkone(d){
if (!d.checked) return; //if it's unchecked, then do nothing
var group=document.getElementById('checkgroup');
var os=group.getElementsByTagName('input');
for (var i=0;i<os.length;i++){
if (os[i].checked&&os[i]!=d) os[i].checked=false;
}
}
Now you can call this function in each checkbox
<div id="checkgroup">
<input id="abcd" onclick="checkone(this);">
<input id="xyz" onclick="checkone(this);">
...
</div>
Note how you don't even need to bother with the name, because the object passes in itself.

Categories