28

I have many checkboxs as below,

<li><input type="checkbox" name="areaofinterest" value="home_coo" id="home_coo" class="Checkbox" > Cooking</li>
    <li><input type="checkbox" name="areaofinterest" value="home_cra" id="home_cra" class="Checkbox"> Crafts</li>
    <li><input type="checkbox" name="areaofinterest" value="home_dec" id="home_dec" class="Checkbox"> Decorating</li>
    <li><input type="checkbox" name="areaofinterest" value="home_ent" id="home_ent" class="Checkbox"> Entertaining</li>
    <li><input type="checkbox" name="areaofinterest" value="home_gar" id="home_gar" class="Checkbox"> Gardening</li>
    <li><input type="checkbox" name="areaofinterest" value="home_hom" id="home_hom" class="Checkbox"> Home Improvement</li>
    <li><input type="checkbox" name="areaofinterest" value="home_mar" id="home_mar" class="Checkbox"> Marriage</li>
    <li><input type="checkbox" name="areaofinterest" value="home_par" id="home_par" class="Checkbox"> Parenting</li>
    <li><input type="checkbox" name="areaofinterest" value="home_pet" id="home_pet" class="Checkbox" > Pets</li>
    <li><input type="checkbox" name="areaofinterest" value="home_ret" id="home_ret" class="Checkbox"> Retirement</li>

How can I use jQuery to get the checked values and output as areaofinterest="home_coo,home_mar,home_pet" ?

Thanks a lot.

6 Answers 6

114

Use the .map() function:

$('.Checkbox:checked').map(function() {return this.value;}).get().join(',')

Breaking that down:

$('.Checkbox:checked') selects the checked checkboxes.

.map(function() {return this.value;}) creates a jQuery object that holds an array containing the values of the checked checkboxes.

.get() returns the actual array.

.join(','); joins all of the elements of the array into a string, separated by a comma.

Working DEMO

Sign up to request clarification or add additional context in comments.

3 Comments

Just an FYI - the "," within the join is not needed as it will add a comma by default when just using join(). Also good to note, this is the exact scenario that jQuery uses in the docs for .map(): api.jquery.com/map
Updated Demo: Make the comma separated string, store the string, then use the string to restore the checkboxes.
can I display it in List??
4
var areaofinterest = '';

$('[name="areaofinterest"]')​.each(function(i,e) {
    var comma = areaofinterest.length===0?'':',';
    areaofinterest += (comma+e.value);
})​;

To get only checked boxes value :

var areaofinterest = '';

$('[name="areaofinterest"]')​.each(function(i,e) {
    if ($(e).is(':checked')) {
        var comma = areaofinterest.length===0?'':',';
        areaofinterest += (comma+e.value);
    }
})​;

FIDDLE

You could also do:

var areaofinterest = [];
$('[name="areaofinterest"]:checked').each(function(i,e) {
    areaofinterest.push(e.value);
});

areaofinterest = areaofinterest.join(',');

And there's probably a bunch of other ways to do this ?

Comments

0
var values = "";
$("checkbox[name=areaofinterest]").each(function() { 
    values += $(this).val() + ",";
});

values = values.substring(0, values.length - 2);

Comments

0

Try something like this:

var areaofinterest= "";

$("input[type=\"checkbox\"]").each(function() {
    if ($(this).attr("checked")) {
        if (areaofinterest !== "") areaofinterest += ",";

        areaofinterest += $(this).value();
    }
});

3 Comments

He wants to sum values not id's
His exact specification was: "How can I use jQuery to get the checked values and output as areaofinterest="home_coo,home_mar,home_pet"?"
In this context his IDs had the same value as his "values", but I see your point. Amended my sample.
0

Or you use can do using array like shown below

 var checkBoxArray = new Array();
 var areaofinterest = '';

 $(':checkbox:checked').each(function(i){
        checkBoxArray .push($(this).attr("value"));

});

  if (checkBoxArray .length == 0) {
     } else {
     while (checkBoxArray .length != 0) {
            if (checkBoxArray .length == 1) {
                areaofinterest += checkBoxArray .pop();
            } else {
                areaofinterest += (checkBoxArray .pop() + ",");
                 }
            }
}
console.log(areaofinterest);

though you will get String in reverse order.

Comments

0
 var mode = [];
            jQuery("input[name='mode[]']:checked").each(function(i) {
                mode.push($(this).val());
            });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.