Almost all the user interfaces that I have created had this functionality of selecting multiple items from a list to process them or delete them. Although its very very easy to implement this functionality in Javascript, using jQuery for this is real fun. I will show you a simple implementation of adding multiple checkbox select and deselect functionality to any webpage. We will have a table with some data in it and checkbox in each row. There will be a select all checkbox in the header of the table. If user select/deselect the selectall checkbox, all the checkbox in table will get selected or deselected accordingly. Now one more thing we would like here to add is, suppose user select all the checkbox one by one then the selectall checkbox should be automatically gets selected. And if user click selectall first and then unchecks any of the checkbox, then the selectall also should be unchecked automatically. Let’s check the code.
The HTML
<HTML>
<HEAD>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<TITLE>Multiple Checkbox Select/Deselect - DEMO</TITLE>
</HEAD>
<BODY>
<H2>Multiple Checkbox Select/Deselect - DEMO</H2>
<table border="1">
<tr>
<th><input type="checkbox" id="selectall"/></th>
<th>Cell phone</th>
<th>Rating</th>
</tr>
<tr>
<td align="center"><input type="checkbox" class="case" name="case" value="1"/></td>
<td>BlackBerry Bold 9650</td>
<td>2/5</td>
</tr>
<tr>
<td align="center"><input type="checkbox" class="case" name="case" value="2"/></td>
<td>Samsung Galaxy</td>
<td>3.5/5</td>
</tr>
<tr>
<td align="center"><input type="checkbox" class="case" name="case" value="3"/></td>
<td>Droid X</td>
<td>4.5/5</td>
</tr>
<tr>
<td align="center"><input type="checkbox" class="case" name="case" value="4"/></td>
<td>HTC Desire</td>
<td>3/5</td>
</tr>
<tr>
<td align="center"><input type="checkbox" class="case" name="case" value="5"/></td>
<td>Apple iPhone 4</td>
<td>5/5</td>
</tr>
</table>
</BODY>
</HTML>
Code language: HTML, XML (xml)
The jQuery Code
Add following jQuery code in HEAD section of above HTML<SCRIPT language="javascript">
$(function(){
// add multiple select / deselect functionality
$("#selectall").click(function () {
$('.case').attr('checked', this.checked);
});
// if all checkbox are selected, check the selectall checkbox
// and viceversa
$(".case").click(function(){
if($(".case").length == $(".case:checked").length) {
$("#selectall").attr("checked", "checked");
} else {
$("#selectall").removeAttr("checked");
}
});
});
</SCRIPT>
Code language: JavaScript (javascript)
Here in above code at line 05, we register a handler on click of selectall checkbox. On click of this checkbox, we check/uncheck all checkbox in the datatable.
Also check the link 13, where we check the selectall checkbox, if all the checkbox are selected manually. To get more idea, play with the below demo.
Great!
One question : Which service you made the initial image of the post?
Thanks
hi
how can u get the values of the items you just ticked?
@shanka, since the check boxes all have the name case, they should be accessible in an array $case[] on the processing page. then you an use the foreach array loop to run through them.
i.e
Is doing it using JAVASCRIPT more easier?
It is really amazing.
@Ram – Thanks :)
Thanks,Thanks a lot.How you became a master in coding?
Wonderful article. Thanks
Its amazing and much easier.
Thank you so much for sharing this.
how u will transfer selected value on php page
thanx for help
very helpful artical…
Perfeito, parabéns e muito obrigado.
Argentina o mais grande do mundo!!
Nice work and thanks for sharing! I wonder whether this is possible :- say the ‘cellphone’ list is only one of many on the page – you might want several of these lists, each in a different category. Is there a way that we could build in a way for the user to collapse/expand categories. Eg., they viewed the cellphone list and would like now to close it (as it takes up real estate), and open another category with its own list and checkboxes. Any help with this would be much appreciated!
i want to delete the particular row on select of checkbox of that row also if all checkboxes are selected all rows should get deleted. this all should happen on the click of down delete button.
@Kirti – This tutorial will show you exactly how to delete row from html table: http://viralpatel.net/dynamically-add-remove-rows-in-html-table-using-javascript/
html file is
Check All
Checkbox 1
Checkbox 2
Checkbox 3
Checkbox 4
Checkbox 5
The Demo For JSP .. help out.. !
it’s good dud…………………………!
Hi all, Can any one please help me to use multi select list box in struts2 tag of Jsp file
Hi Frds,
Its really amazing.Thanks a lot for help me to finding my solution.
This Is Very Useful i am php programmer and i used this logic of you rather than plugins
Nice Sharing Thank you Very much, i want this script thank you….
Fana.
Very Useful Post. Gr8 work. Very Nice :)
OMG… i’m search this kind of function… for 2 hours.. this is the exactly that i’ve looking for.. thanks Developer for sharing this code.. HURRAY!!!
Great successive code brother.
thank you !!!
:) Cheers :)
Really helpful article….
Thanks brother..
Thanks… Nice job..
Thanks man..you did the great job…
this is really amazing. This helps me a lot……. Thanks a lot.
can you tell me how do I proceed for deleting this value from database and from table too.
If you have example for it share the link
Thank you budy for the nice tutorial.. you made my day ….. thanks once more..
this is fantastic script!!.. you light my world!..
the vice-versa snippet is the champs! :)
This is exactly what I have looking… marvelous
Great Job…
Thank You…
Your code do not work perfectly.
please modify this code.
Thanks a ton!!
Great coding!!
hi i have issue. iam looping check boxes with id “#membership_role_ids_” whith you are same logic. iam able to select only first check box. what is issue. please tell me ..
Good job mate
Tricky :)
Good work . thanks for this code.
Hi,
Thanks for this great code, saved much of my time ;)
Regards,
Nickhil4u
Its not working on firefox 17.0
It does works for me!! Check if jQuery is properly included and you have access to http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
Wow exactly working great and what i am searching for..thanks…
FANTASTIC!!!!
Excellent !!! Job
Really helpful
nice
u rocked
Dude thanks for the code. It is working for Struts2 also.
Thanks a lot.
Liked .. very good thinking…
Hi,
nice work dude, but explain the code too.
Great help.
But when i dynamic this code,first iteration code working is fine.In second code not work.Any idea.
I used another method to tick for a check box.
replace line $(“#selectall”).trigger(“click”); instead of $(“#selectall”).attr(“checked”, true);
Thanks
superb
I have used this in my project but, i am not able get value of single row of checkbox if there is only single record.
Its working fine for more than one record.
unable trace out the problem.
Hi, i use this code is working ,but in first time .
I modify this same code is working very well pls check below example
Example
// add multiple select / deselect functionality
$(“#selectall”).click(function() {
$(‘.case’).prop(‘checked’, this.checked);
});
// if all checkbox are selected, check the selectall checkbox
// and viceversa
$(“.case”).click(function() {
if ($(“.case”).length == $(“.case:checked”).length) {
$(“#selectall”).prop(‘checked’, true);
} else {
$(“#selectall”).prop(‘checked’, false);
}
});
jQuery(‘.checkbox_all’).click(function()
{
jQuery(‘.checkbox1’).prop(‘checked’,jQuery(‘.checkbox_all’).prop(‘checked’));
jQuery(‘.checkbox1’).click(function()
{
jQuery(‘.checkbox_all’).removeAttr(‘checked’);
});
});
Thank you man. I am a programmer form Afghanistan. I have used from your code and it works completely for me.
Best of Luck
This is awesome!! Really helpful for me and developers. Thanks for sharing with us. Following links also helped me to complete my task.
http://www.mindstick.com/Articles/2f772095-325f-4996-97a6-015f9c00a430/?Select%20All%20CheckBox%20from%20Header%20CheckBox
http://www.codeproject.com/Articles/23256/Selecting-Deselecting-all-the-CheckBoxes-Inside-a
Thank you so much!
I already had looking for this in many sites.
I found some but not clear like this.
Thanks for sharing your thoughts on jquery. Regards
Thanks for the info,
Thanks dude..So nice, this one solved my prob..
Hi… this sample is really great…
you can also consider my code below base on your tutorial.. hope its also help..
—————————————————————————————
—————————————————————————————
Thanks a lot Viral..
Thanks guys for the nice code. This is a nice improvement, .prop() works better for me when toggling the check box selection.
@Roldan Unne yours code worked for me
Thanks man ! It really helped me !
thnx man , keep it up
In my project jquery is not working could you please update same thing using javascript.
Hi. nice. But there is one more case, when any one of the child checkbox hets unchecked then master checkbox should be unchecked right ? for this i have written a post. please look at the code in this post: http://coding-issues.blogspot.in/2013/10/check-uncheck-all-checkboxes-jquery.html
it works only single time, we need refresh page each time,
Use .prop instead of .attr
that should fix it!
Thanks, this code worked for me.
500
6000
6540
Hi!
This co
Thanks you.This code is used in my project.its perfectly working.Good job
This will not work with the jquery 1.10.2 version.
Check the below link for solution
http://stackoverflow.com/questions/5874652/prop-vs-attr
i’m also having same problem.do i have to change the jquery code for js 1.10.2.kindly give me some advise
raghuveer: this solution completely works with js 1.4.2.min.js but why it can’t work with 1.10.2 js?i read your provided link but didn’t understand the sollution.can you give a simple sollution to this?waiting for your reply
i am also having same problem….which jquery plugin i have to use
thanks brother
Jquery for counting selected checkboxes with same classes
$(function() {
$(“.case”).click(function(){
var c =0;
$(“.case:checked”).each(function(){
c++;
});
$(“#totParts”).val(c);;
});
});
Very frustrating script when its being adapted, but one tip – that script will not work as is with jquery 2.0.3, you have to use prop, else it’ll just work once and then quit. this I what I ended up with:
Thanks it helped a lot.
ya thanks /// its working !!!!!!!!!!!!!!!!
very good
thanks
I have multiple checkboxes with same id “RoleType_1” now on click function how can i disable all the checkboxes with that id “RoleType_1”
Great..!!! Its working well. Thank you very much bro…
Hi,
Thanks to your code. Just want to ask how to make it work if I can only work on input name and type. Because, I cannot make edits to the form code as it is system generated.
My code:
$(function(){$(“input[name=AllCommunication]”).click(function(){$(“input[type=checkbox]”).attr(‘checked’,this.checked);});$(“input[type=checkbox]”).click(function(){if($(“input[type=checkbox]”).length==$(“input[type=checkbox:checked]”).length){$(“input[name=AllCommunication]”).attr(“checked”,”checked”);}else{$(“input[name=AllCommunication]”).removeAttr(“checked”);}});});
Needs Help On This: When we deselect one of the checkboxes, the Select All checkbox should be uncheck.
Please help me on this. Thanks!
Thanks.
Is there any way for this code to take in a variable?
So rather than having for strictly case, it will take in what ever x is.
‘thead> th.multiSelect:first’
pls give any other suggestions for this query
Reiterating what “Richard” posted here already,
Using Prop instead of attr works, dont ask me why and how, new version of jQuery library seems to NOT like it.
Happy coding:
// add multiple select / deselect functionality
$(“#selectAll”).click(function () {
var checkAll = $(“#selectAll”).prop(‘checked’);
if (checkAll) {
$(“.case”).prop(“checked”, true);
} else {
$(“.case”).prop(“checked”, false);
}
});
// if all checkbox are selected, check the selectall checkbox
// and viceversa
$(“.case”).click(function(){
if($(“.case”).length == $(“.case:checked”).length) {
$(“#selectAll”).prop(“checked”, “checked”);
} else {
$(“#selectAll”).prop(“checked”);
}
});
Use this change $j to $ …. i use because Jquery conflict
$j(function () {
// add multiple select / deselect functionality
$j(“#selectall”).click(function () {
if (this.checked == true) {
$j(‘.case’).attr(‘checked’, “checked”);
$j(‘.case’).prop(“checked”, true);
}
else {
$j(‘.case’).removeAttr(“checked”);
$j(‘.case’).prop(“checked”, false);
}
});
// if all checkbox are selected, check the selectall checkbox
// and viceversa
$j(“.case”).click(function () {
if ($j(“.case”).length == $j(“.case:checked”).length) {
$j(“#selectall”).attr(“checked”, “checked”);
} else {
$j(“#selectall”).removeAttr(“checked”);
}
});
});
My solution, tested with jquery-3.3.1.
$(function () {
// add multiple select / deselect functionality
$(“#selectall”).change(function () {
$(“.case”).prop(“checked”, this.checked);
});
// if all checkboxes are selected, check the selectall checkbox otherwise uncheck
$(“.case”).click(function () {
if ($(“.case”).length === $(“.case:checked”).length) {
$(“#selectall”).prop(“checked”, true);
} else {
$(“#selectall”).prop(“checked”, false);
}
});
});