Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

show in a div or p what check boxes have been checked

i want to show in a div what check boxes have been checked

for example

<input id="checkbox1" type="checkbox"><label for="checkbox1">Checkbox 1</label>

<p>you have selected:</p> <div>checkbox 1</div>

there will be more than 1 check box (37) and i need it to be able to select all the check boxes

i think that make sense

Thanks

like image 684
Daniel Edwards Avatar asked Mar 14 '23 20:03

Daniel Edwards


2 Answers

You can use:

$(":checkbox").change(function(){
   $('div').html($(':checked').map(function(){
      return $(this).next().text()
   }).get().join(','));
});

Working Demo

like image 95
Milind Anantwar Avatar answered Mar 20 '23 10:03

Milind Anantwar


This is how I would do it:

$("input[type='checkbox']").change(function(){
    $("#checked").empty();
    $("input[type='checkbox']:checked").each(function(){
        $("#checked").html($("#checked").html() + $(this).next().text() + "<br/>");
    });
});

Here is the JSFiddle demo

like image 43
Ahs N Avatar answered Mar 20 '23 12:03

Ahs N