I have a checkbox on the page and let's say that when the page is loaded, I need it to press on, a simple attribute substitution will give nothing, since you need to run functions that are assigned to the checkbox. I tried

$('.checkbox').trigger('change'); $('.checkbox').trigger('click'); $('.checkbox').change(); 

but none of the methods works.

  • one
    Can't you just add the checked attribute to the checkbox? - hUuueeeyy
  • $('.checkbox').prop('checked', true) - Raz Galstyan

1 answer 1

To define \ set checked DO NOT USE anything other than .prop() .

Read more about .prop()

 $('input[type="button"').on('click',function(){ $('.onload').each(function(){// Переберём var thisProp = $(this).prop('checked'); // вернёт true - если "выбран", ну и false если нет. $(this).prop('checked', thisProp ? false : true); // Инвертируем значение. }); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <!-- ----------------------------- --> <input type="checkbox"> - изначально ВЫКЛ<br> <input type="checkbox" checked> - изначально ВКЛ<br> <input class="onload" type="checkbox"> - изначально ВЫКЛ, после загрузки ВКЛ<br> <input class="onload" type="checkbox" checked> - изначально ВКЛ, после загрузки ВЫКЛ <br><br><br><br> В примере, вместо загрузки страницы, будем использовать эту кнопку.<br> <input type="button" value="типо загрузим страницу">