1

I want to get the values of checked checkboxes with the name="car_type[]" and alert the final array with all the values. How to do that? So far, I have this code. But it's not working. I don't know how to loop through checked checkboxes properly and add the values to the array car_type_arr. I also cannot alert the final array using alert(car_type_arr);. Thanks for any help.

$().ready(function(){

        $('.prettycheckbox').click(function(e) {    

            e.preventDefault();

            var car_type_arr = [];

                $("input:checkbox[name=car_type]:checked").each(function()
                        {
                             // here I need to add values to array like in php
                             // e.g. $car_type_arr[] = $the_grabbed_value;
                             // but using javascript syntax
                             // How to do that?
                        });  

            // then I need to alert the array, how to do that in JS?
            alert(car_type_arr);

            return false;
        });

});
1
  • You can use car_type_arr.push(<value>); Commented Jul 31, 2013 at 7:28

5 Answers 5

2

You can use map

var car_type_arr = $("input:checkbox[name=car_type]:checked").map(function() {
                        return this.value;
                   }).get();

console.log(car_type_arr);
Sign up to request clarification or add additional context in comments.

Comments

0

Try below code:

<input type="checkbox" name="chkBox" value="xxx">
<input type="checkbox" name="chkBox" value="xxx1">
<input type="checkbox" name="chkBox" value="xxx2">
<input type="button" id="btnBox">

$(document).ready(function(){
$("#btnBox").click(function(){
        var car_type_arr = [];

        $("input:checkbox[name=chkBox]:checked").each(function() {                
            car_type_arr.push($(this).val());
            alert(car_type_arr);
    }); 
});
});

Demo: http://jsfiddle.net/XDpBP/

Comments

0

Try this:

$("input:checkbox[name=car_type]:checked").each(function()
    {
        car_type_arr.push($(this).val());
    });

Comments

0

your code should look like,

$().ready(function(){

        $('.prettycheckbox').click(function(e) {    

            e.preventDefault();

            var car_type_arr = [];

                $("input:checkbox[name=car_type]").each(function()
                        {

                              if($(this).is(':checked')){
                                car_type_arr.push($(this).val());
                              }
                        });  

            alert(car_type_arr);

            return false;
        });

});

Comments

0
$("input:checkbox[name=car_type]:checked").each(function() {
    car_type_arr.push($(this).val());
}); 

1 Comment

$("input:checkbox[name=car_type]").is(":checked") will return boolean value, then how would you call .each() on boolean value.

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.