1

I have tried to do a test:

var test = Array();

$('input[name=first_category[]]:checked').each(function()
{
    test.push($(this).val());
});

alert(test);
  1. Check the last checkbox.
  2. Refresh the page.
  3. Alert should be empty (but is not empty, it shows me 4).

I have 5 categories and each category contains more than 10 checkboxes.

1 Answer 1

3

Sorry, I have updated my solution. check here for the updated solution.

<input type="checkbox" name="first_category[]" value="1" />
<input type="checkbox" name="first_category[]" value="2" /> 
<input type="checkbox" name="second_category[]" value="3" /> 
<input type="checkbox" name="second_category[]" value="4" />
<br/>
<input type="button" value="check" id="btnCheck"/>

$("#btnCheck").live('click', function() {
    var test = [];
    $("input[name='first_category[]']:checked").each(function() {
        test.push($(this).val());
    });
    alert(test);
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your answer! What I want is to alert only the checkboxes from first_category[].
A link to an external site is not an acceptable answer. I have copied your code from jsfiddle back into your answer. (pending peer review)

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.