Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to check dynamically created checkbox is checked using jquery

$("input[type='checkbox']").change(function(){
  if(this.checked){
    if($(':checkbox:checked').length>1 && $('#qT').val()==1){
        $(this).prop( "checked", false );
    }
  }
});

this is the script to run
And html looks like below

<div class=row>....
<div class="isCorrect">
    <span>correct</span>
    <input type="checkbox" class="form-check-input mx-1"value="1">
</div>...</div>
<div class=row>....
<div class="isCorrect">
     <span>correct</span>
     <input type="checkbox" class="form-check-input mx-1"value="1">
</div>...</div> 
etc etc

This code only checks the first check box. not any other..
I also tried

$("input[type='checkbox']").change(function(){
  if(this.checked){
    if($(':checkbox:checked').length>1 && $('#qT').val()==1){
        $(this).prop( "checked", false );
    }
  }
}).triger(change);

and many other ways

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your code is not working because your event handler is created before the dynamic checkboxes are. To bypass this, you can bind an event handler to the whole document or, for better performance, to the parent that contains all your checkboxes :

$(document).on('change',"input[type='checkbox']",function () {

    // your code here...

});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading