Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check multiple check boxes in jQuery? [duplicate]

I have a table with a checkbox at the start of each row. Each Checkbox has the ID of #tablecheckbox. The table header row has a check icon which should check all boxed in the table. How can I do this with jQuery?

like image 853
JT Nolan Avatar asked Aug 16 '13 05:08

JT Nolan


People also ask

How can I get multiple checkboxes checked in jQuery?

for(var i = 0; i < values. length; i++) $("#list [value=" + values[i] + "]"). attr("checked", "checked");

How can I check all checkboxes within a div using jQuery?

click(function(){ $(':checkbox'). prop("checked", true); alert("1"); }); $('#deselectChb'). click(function(){ $(':checkbox'). prop("checked", false); alert("2"); });


1 Answers

Here head_checkbox is id of top header and person class is all row checkbox

 $('#head_checkbox').on('change', function () {
            if ($(this).is(':checked')) {
                $('.person').attr('checked', true);
            } else {
                $('.person').attr('checked', false);
            }
        });

        $('.person').click(function () {
            var total_length = $('.person').length;
            var total_checked_length = $('.person:checked').length;

            if (total_length == total_checked_length) {
                $('#head_checkbox').attr('checked', true);
            } else {
                $('#head_checkbox').attr('checked', false);
            }
        });
like image 167
Jay Hardia Avatar answered Oct 05 '22 10:10

Jay Hardia