Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: How to catch change event from input[type='checkbox'] if it was checked programmatically?

I've got an <input type="checkbox" id="check1" />. How to catch change event for this checkbox if it was changed from script i.e. $('#check1').attr('checked', 'checked');?

Thanks.

like image 432
foreline Avatar asked Jan 29 '11 12:01

foreline


People also ask

How do I check if a checkbox is checked in an event?

addEventListener('click', event => { if(event. target. checked) { alert("Checkbox checked!"); } });

How check if checkbox is checked jQuery?

prop() and is() method are the two way by which we can check whether a checkbox is checked in jQuery or not. prop(): This method provides an simple way to track down the status of checkboxes. It works well in every condition because every checkbox has checked property which specifies its checked or unchecked status.

Does Onchange work for checkbox?

The onchange event occurs when the value of an element has been changed. For radiobuttons and checkboxes, the onchange event occurs when the checked state has been changed.

When checkbox is select which event is generated?

When the user selects a checkbox, the CheckBox object receives an on-click event. To define the click event handler for a checkbox, add the android:onClick attribute to the <CheckBox> element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event.


1 Answers

Javascript doesn't fire events based on programmatic changes to form elements - to prevent infinite loops of events - so you have two (less-than-ideal) options:

  • Write a wrapper function which changes the attribute and calls your callback, and then force yourself to always use your wrapper function
  • Just call the callback yourself when you change the attribute
like image 97
Gareth Avatar answered Oct 29 '22 10:10

Gareth