user1537326
user1537326

Reputation:

multiple select checkbox in jquery

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);

Upvotes: 3

Views: 1022

Answers (2)

user1537326
user1537326

Reputation:

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

Upvotes: 0

Ram
Ram

Reputation: 144659

try this:

function countChecked() {
  var n = 0;
  $(":checkbox:checked").each(function(){
     n += parseInt(this.value, 10)
  })
  $("div").text(n + (n === 1 ? " is" : " are") + " checked!");
}

Upvotes: 2

Related Questions