3

I want to show the multiple value of checkbox in a div or textbox.

function countChecked() {
    var n = $("input:checked").val();
    $("div").text(n + (n === 1 ? " is" : " are") + " checked!");
}
countChecked();
$(":checkbox").click(countChecked);

2 Answers 2

2

try this:

function countChecked() {
  var n = 0;
  $(":checkbox:checked").each(function(){
     n += parseInt(this.value, 10)
  })
  $("div").text(n + (n === 1 ? " is" : " are") + " checked!");
}
Sign up to request clarification or add additional context in comments.

1 Comment

this output length .. i want value.
0
function countChecked() {
    var n= "";
    $("input[type=checkbox]:checked").each(function(){
        n += $(this).val() + " ";
    });
    $("#change").text(n);
}
countChecked();
$(":checkbox").click(countChecked);

1 Comment

You should add some kind of explanation of what you did there to improve his code. This site is about learning, not just about solutions. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.