0

I have a button which on click adds checkbox and text fields. Also I'm counting the checked boxes - which doesn't seem to be working. So I added 2 more "static" ones and it works. The strange thing is the one added through appendTo() are only being counted after I click on the "static" boxes. What I mean, for example Click on appendTo() checkbox - 0 counted Click a "static" one - It shows two, which is correct.

$(function() {
   // other stuffs
   function countChecked() {
          var n = $(":checked").length;
          $("#a").text(n + (n === 1 ? " is" : " are") + " checked!");
   }
   countChecked();
   $(":checkbox").click(countChecked);

    $( "#addanswer" ).click(function() { 
                clicked=clicked+1;
                $('<input>').attr({
                    type: 'checkbox',
                    id: 'markup['+clicked+']',
                    name: 'markup['+clicked+']'
                }).appendTo('#myForm').after("");               
                $('<input>').attr({
                    type: 'text',
                    id: 'answer['+clicked+']',
                    name: 'answer['+clicked+']'
                }).appendTo('#myForm').after("<br />");
            });    
          // ...
          // ...

Anyone can tell me what is wrong? I have tried already varoius ways to count.

1 Answers1

1

Use .on instead.

$('#myForm').on('click', 'input:checkbox', countChecked);
xdazz
  • 158,678
  • 38
  • 247
  • 274