Nolik
Nolik

Reputation: 4523

How get all values from all inputs?

I have Inputs

<input name="v[]" value="1" type="checkbox">
<input name="v[]" value="2" type="checkbox">
<input name="v[]" value="3" type="checkbox">
<input name="v[]" value="4" type="checkbox">

Where checked with this code

$(document).on('click',"tbody tr", function() {
    if ($(this).hasClass('info')) {
        $(this).removeClass('info');
        $(this).find('input[name="v[]"]').prop('checked', false);
    } else {
        $(this).addClass('info');
        $(this).find('input[name="v[]"]').prop('checked', true);
    }
});

I want get array of checked Inputs with their values. How i can do it?

Thanks.

Upvotes: 0

Views: 112

Answers (3)

subhaze
subhaze

Reputation: 8855

.serializeArray should do it

$('input').on('click', function(){
    console.log($('input').serializeArray());
});

http://jsfiddle.net/cbm4T/

Example of just getting inputs of type checkbox http://jsfiddle.net/cbm4T/1/

$('input').on('click', function(){
    console.log($('input[type=checkbox]').serializeArray());
});

Upvotes: 0

james emanon
james emanon

Reputation: 11807

var values = $.map($('input[name="v[]"]:checked'), function(obj) {
              return $(obj).val();

});

edited: others beat me to it.

AND

if for some reason you wanted the reverse. checkboxes that are not clicked and their values.

var values = $.map($('input[name="v[]"]:not(:checked)'), function(obj) {
              return $(obj).val();

});

Upvotes: 0

adeneo
adeneo

Reputation: 318182

var values = $.map($('input[name="v[]"]:checked'), function(el,i) { return el.value });

FIDDLE

Upvotes: 3

Related Questions