68

How can I get the values of checkboxes which are selected using jQuery?

My HTML code is as follows:

<input  id="ad_Checkbox1" class="ads_Checkbox" type="checkbox" value="1" />
<input  id="ad_Checkbox2" class="ads_Checkbox" type="checkbox" value="2" />
<input  id="ad_Checkbox3" class="ads_Checkbox" type="checkbox" value="3" />
<input  id="ad_Checkbox4" class="ads_Checkbox" type="checkbox" value="4" />
<input type="button" id="save_value" name="save_value" value="Save" />
0

13 Answers 13

138

Try this

<input name="selector[]" id="ad_Checkbox1" class="ads_Checkbox" type="checkbox" value="1" />
<input name="selector[]" id="ad_Checkbox2" class="ads_Checkbox" type="checkbox" value="2" />
<input name="selector[]" id="ad_Checkbox3" class="ads_Checkbox" type="checkbox" value="3" />
<input name="selector[]" id="ad_Checkbox4" class="ads_Checkbox" type="checkbox" value="4" />
<input type="button" id="save_value" name="save_value" value="Save" />

function

    $(function(){
      $('#save_value').click(function(){
        var val = [];
        $(':checkbox:checked').each(function(i){
          val[i] = $(this).val();
        });
      });
    });
Sign up to request clarification or add additional context in comments.

2 Comments

how to display this? I tried document.getElementById("#mydiv").innerHTML = i; but it only shows one result, how to display it with all the checked results?
@Gosi innerHTML replaces the content. "i" also represents an int
76

To get an array of values from multiple checked checkboxes, use jQuery map/get functions:

$('input[type=checkbox]:checked').map(function(_, el) {
    return $(el).val();
}).get();

This will return array with checked values, like this one: ['1', '2']

Here is working example on jsfiddle: http://jsfiddle.net/7PV2e/

4 Comments

Why not use the first parameter of map callback "_" ?
@JuniorUsca: First parameter of jQuery's map method is index of current element (api.jquery.com/map)
what does the "_," in the map function arguments signify?
@hendy0817: _ was the convention to mark that the first parameter will not be used in the body of the function. _ is a valid variable name. I could call first parameter thisParameterWillNotBeUsed, index or _unused, but naming it just _ was a convenient way to do.
28

You can do it like this,

$('.ads_Checkbox:checked')

You can iterate through them with each() and fill the array with checkedbox values.

Live Demo

To get the values of selected checkboxes in array

var i = 0;
$('#save_value').click(function () {
       var arr = [];
       $('.ads_Checkbox:checked').each(function () {
           arr[i++] = $(this).val();
       });      
});

Edit, using .map()

You can also use jQuery.map with get() to get the array of selected checkboxe values.

As a side note using this.value instead of $(this).val() would give better performance.

Live Demo

$('#save_value').click(function(){
    var arr = $('.ads_Checkbox:checked').map(function(){
        return this.value;
    }).get();
}); 

1 Comment

.map(function() always work for multiple values, @Adil thanks that's so helpful
8
$('input:checked').map(function(i, e) {return e.value}).toArray();

Comments

8

You can get them like this

$('#save_value').click(function() {
    $('.ads_Checkbox:checked').each(function() {
        alert($(this).val());
    });
});

jsfiddle

4 Comments

You should really post the code along with your answer. Say jsfiddle went down, or died... this answer would be worthless. It would also help people find the answer who are searching through google, etc.
there are thousand's of answers which are using jsfiddle on SO and jsfiddle is the best way to see the things in working manner to provide better answer to question
Using jsfiddle is great, please do so, but please also post the answer within Stackoverflow! Like I said, say jsfiddle disappeared from the internet, and someone with the same problem found this post... there would be no answer just a dead link. It would also not be indexed by search engines.
ok i will keep this in mind and i have also changed the answer
3

You may try;

$('#save_value').click(function(){
    var final = '';
    $('.ads_Checkbox:checked').each(function(){        
        var values = $(this).val();
        final += values;
    });
    alert(final);
});

This will return all checkbox values in a single instance.

Here is a working Live Demo.

Comments

1

Since u have the same class name against all check box, thus

   $(".ads_Checkbox") 

will give u all the checkboxes, and then you can iterate them using each loop like

$(".ads_Checkbox:checked").each(function(){
   alert($(this).val());
});

Comments

1

Also you can use $('input[name="selector[]"]').serialize();. It returns URL encoded string like: "selector%5B%5D=1&selector%5B%5D=3"

Comments

1

try this one.. (guys I am a new bee.. so if I wrong then I am really sorry. But I found a solution by this way.)

var suggestion = [];
$('#health_condition_name:checked').each(function (j, ob) {

    var odata = {
        health_condition_name: $(ob).val()
    };

    health.push(odata);
});

Comments

0

Try getPrameterValues() for getting values from multiple checkboxes.

Comments

0
$(document).ready(function() {
    $("#save_value").click(function(){
        var arr = [];
        $.each($("input[class='ads_Checkbox']:checked"), function(){
            arr.push($(this).val());
        });
        alert("Your selected options are: " + arr.join(","));
        });
});

Comments

0

You need this:

var allCheckboxesChoosed = [];  
$("input[name='myCheckbox[]']:checked").each(function(){  
    var oneCheck = {  
        checkId: $(this).attr("id"),  
        value: $(this).val()  
    };  
    allCheckboxesChoosed.push(oneCheck);  
});

console.log('allCheckboxesChoosed', allCheckboxesChoosed);

Comments

-1

$(document).ready(function(){
$(".chk").click(function(){
var d = $("input[name=test]"); if(d.is(":checked")){
//
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" name="test"> `test1`
<input type="checkbox" name="test"> `test2`
<input type="checkbox" name="test"> `test3`
<input type="button" value="check" class='chk'/>

1 Comment

While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.