0

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.

3 Answers 3

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

FIDDLE

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

3 Comments

I get Uncaught TypeError: Object has no method 'get' in a jsFiddle using jQuery 1.8.3 or 1.9.1
Now it work, but this code getting ALL values from ALL inputs, while i want get ALL values from ALL inputs WHERE checked.
Was'nt paying attention to this, but @SheikhHeera fixed it, Thanks!
0
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();

});

Comments

0

.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());
});

2 Comments

What if there are other inputs on the form ?
You would just need to update the selector to be more specific. I've added an example in my answer.

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.