Wednesday, June 5, 2024
 Popular · Latest · Hot · Upcoming
176
rated 0 times [  180] [ 4]  / answers: 1 / hits: 16029  / 11 Years ago, mon, october 21, 2013, 12:00:00

boxes checked using jQuery prop() do not affect listeners attached to change handler.



My code is something like



HTML



<div>
<label>
<input type=checkbox class=ch />test</label>
<label>
<input type=checkbox class=ch />test</label>
<label>
<input type=checkbox class=ch />test</label>
<input type=button value=check the box id=select />
</div>


JS



 $(body).on(change, .ch, function(){

alert(checked);

});


$(body).on(click, #select, function(){

$(this).parent(div).find(input[type=checkbox]).prop(checked, true);

});


the alert fires when I click on the checkbox. How can I make it fire when the property of the checkbox changes? JSBIN


More From » jquery

 Answers
6

You have to use .change() to trigger the change event listener:



$(body).on(change, .ch, function () {
alert(checked);
});


$(body).on(click, #select, function () {
$(this).parent(div).find(input[type=checkbox]).prop(checked, true).change();
});


JSBbin or Fiddle



Please note that this will fire many events. Three in the html example you have in jsBin.


[#74823] Sunday, October 20, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
griffinr

Total Points: 242
Total Questions: 91
Total Answers: 105

Location: Indonesia
Member since Wed, Jul 7, 2021
3 Years ago
;