JQuery - updating attr on a checkbox does not raise a change event

I have a couple of flags and a status field that indicates whether at least 1 of the flags is checked.

I also have an All / No flag that works when switching flags, but it does not trigger a change event that is assigned to each of these flags, so the status is never updated if the All / No flag is selected.

Here is my implementation:

http://jsfiddle.net/axl163/Fckvd/1/

+6
source share
2 answers
function toggleChecked(status) { $(".chkbx").each( function() { $(this).prop("checked", status).change(); // you have to trigger the event }); } 

Demo

+13
source

Any changes to the input element through the script do NOT fire the onchange event.

Alternatively, you can run change() when updating the input, as shown below,

 $(this).prop("checked", status).change(); 

Full code:

 function toggleChecked(status) { $(".chkbx").each( function() { $(this).prop("checked", status).change(); }); } 

Also use .prop instead of .attr

Demo

+5
source

Source: https://habr.com/ru/post/916596/


All Articles