Check dynamically created checkboxes in case a condition is true

0

Hello! I have a problem that I can not solve. I want to "check" the checkboxes that are generated dynamically in my HTML code. The structure works like this:

1. The page loads;   2. By clicking on a particular button, a dynamic modal is generated;   3. All checkboxes contained in this modal have the same class (' .checkbox');

Now that's the problem! I need to leave the checked property of each checkbox as true if it satisfies a condition that I will make. But I can not detect this checkbox and insert those changes. I have already used the on () method and I still can not manipulate these checkboxes.

How can I accomplish this with JQuery?

    
asked by anonymous 27.11.2017 / 21:39

1 answer

0

You can do this by adding true to all checkbox with class .checkbox :

$('#elemento').on('click', function(){
   if(1 == 1){ // condição
      $('.checkbox').prop('checked', true);
   }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputtype="checkbox" class="checkbox" />
<input type="checkbox" class="checkbox" />


<input type="button" id="elemento" value="Checar tudo" />
    
27.11.2017 / 22:02