jQuery: Get all selected Checkbox values and save in array.

/ / 0 Comments

Overview: Get all selected checkbox values jQuery. This post explains how to get all the ids of selected checkboxes and stored them in an array variable. 

First, we use jQuery .each() method which iterates over all checkboxes, then we use jQuery .is(“:checked”) which returns a boolean value i.e whether it's checked or unchecked.  

If it's checked .i.e return “true”  then we use jQuery  .attr() method to fetch the Id attribute of the corresponding checkbox and stored it in an array variable, here ‘arr’ is our array variable.    

jQuery Code to get all selected Checkbox values:

$("#btnGetAllSelected").on('click', function () {
    var arr = [];
    $("input[type=checkbox]").each(function () {
        var self = $(this);
        if (self.is(':checked')) {
            arr.push(self.attr("id"));
        }
    });
    console.log(arr);
}); 

 VIEW DEMO   

Conclusion: Here using jQuery .each() and .attr() we are able to get all selected checkbox values, and save them into an array variable.

Thank you for reading, pls keep visiting this blog and share this in your network. Also, I would love to hear your opinions down in the comments.

PS: If you found this content valuable and want to thank me? 👳 Buy Me a Coffee

Subscribe to our newsletter

Get the latest and greatest from Codepedia delivered straight to your inbox.


Post Comment

Your email address will not be published. Required fields are marked *

0 Comments