Mark dynamically created checkboxes in case a condition is true

Asked

Viewed 109 times

0

Hello! I have a problem that I am not able to solve. I want to "check" the checkbox that is generated dynamically in my HTML code. The structure works like this:

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

Now here’s the problem! I need to leave the checked property of each checkbox as true if you meet a condition I will make. But I cannot detect this checkbox and insert these changes. I’ve already used the on() method and I still can’t handle these checkboxes.

How can I do this with Jquery?

  • You’ll get better answers by giving people code they can use to reproduce the problem. https://answall.com/help/mcve

1 answer

0

You can do it this way by adding true to all the 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>
<input type="checkbox" class="checkbox" />
<input type="checkbox" class="checkbox" />


<input type="button" id="elemento" value="Checar tudo" />

Browser other questions tagged

You are not signed in. Login or sign up in order to post.