Checkbox get and set values in jquery

Overview: Get 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 $.attr() 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

 

Sharing is Sexy:    
Satinder Singh

Hi all, am Founder and Author of Codepedia.info, also atypical polyglot programmer from Bombay, India. I have a passion to create, solve, and deploy software applications.
       ForEach ( minute in MyLife ) { myExperience++ ; }



Post Comment


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