1

I have checkbox with the same name ,I need to get the selected value of checkbox in a array.(It should exclude non selected value). I have following code.

$(document).ready(function() {
    $(".t").change(function() {
        var allVals = [];
        $("input[name='checkbox1']:checked").each(function() {
            // alert($(this).val());
            allVals.push($(this).val());
        });
        $('#click').click(function() {
            $.each(allVals, function() {
                alert(this); // It alerts old past value also
            });
        });
    });
});


<input name="checkbox1" class="t" type="checkbox" value="1"/>a <br/>
<input name="checkbox1" class="t" type="checkbox" value="2"/>b <br/>
<input name="checkbox1" class="t" type="checkbox" value="3"/>c <br/>
<input name="checkbox1" class="t" type="checkbox" value="4"/>d <br/>
<input id="click" id="button" type="button" value="click" />

Here is a JSFiddle with my code -> http://jsfiddle.net/manseuk/VHSrZ/

Can anyone suggest how to solve it ?

2
  • 1
    What error value? We can't all read minds, even if you can. Commented Nov 25, 2011 at 14:29
  • I should get value of checkbox current selection. In my case It get printed all previous non selection also Commented Nov 25, 2011 at 14:31

3 Answers 3

2

You could do:

$(document).ready(function() {
    var allVals;
    $(".t").change(function() {
        allVals = [];
        $("input[name='checkbox1']:checked").each(
        function() {
            allVals.push($(this).val());
        });

    });
});

In this way allVals is an array that contains only checked values

Fiddle here http://jsfiddle.net/K6dTF/ (use firebug to check the results)

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

Comments

1

You are binding a new click function each time you click a checkbox. You need to put this out of this scope, or unbind the function and rebind.

Something like:

$(document).ready(function() {
    var allVals = [];
    $(".t").change(function() {
        allVals = [];
        $("input[name='checkbox1']:checked").each(function() {
            // alert($(this).val());
            allVals.push($(this).val());
        });
    });
    $('#click').click(function() {
        $.each(allVals, function() {
            alert(this); // It alerts old past value also
        });
    });
});

See fiddle: http://jsfiddle.net/VHSrZ/3/

Comments

1

Change your JS to the following :

It will alert an array of selected values

$('#click').click(function() {
    var allVals = [];
    $("input[name='checkbox1']:checked").each(function() {
        // alert($(this).val());
        allVals.push($(this).val());
    });
    alert(allVals);
});

Working example : http://jsfiddle.net/manseuk/VHSrZ/2/

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.