6

jQuery: Get all selected checkbxes values - Codepedia

 2 years ago
source link: https://codepedia.info/get-selected-checkbox-value-in-jquery
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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

Satinder Singh / August 24, 2021 / 0 Comments

Overview: Get all selected checkbox values jQuery. This post explain how to get all the ids of selected checkbox and stored it in a array variable. First we use $.each() which loop over all checkboxes , then we use $.is(“:checked”) which returns boolean value i.e whether its checked or unchecked.  If its checked .i.e return “true”  then we use jQuery  $.attr() method to fetch ID attribute of corresponding checkbox and stored it in array variable , here ‘arr’ is our array variable.

 

 

JQUERY:

$("#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 $.each() and .attr() we able to get all selected checkbox values.

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 do a favor, then 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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK