I've got an event listener on a checkbox:
<input type="checkbox" name="something">
My event listener:
$('input[type="checkbox"][name="something"]').change(function() { //DO SOMETHING });
I have another event listener that changes .prop
of the checkbox:
$('#button').click(function() { $('input[type="checkbox"][name="something"]').prop("checked", false); });
When I check the checkbox DO SOMETHING
triggers. When I click on the #button
, the .prop
changes and I see the checkbox visually uncheck, but DO SOMETHING
doesn't get triggered...
Something I'm overlooking?
When you dynamically set a value in a textfield using jQuery . val(), you have to manually trigger the . change event if you want to add extra code that trigger when the value of the field change.
jQuery trigger() Method The trigger() method triggers the specified event and the default behavior of an event (like form submission) for the selected elements. This method is similar to the triggerHandler() method, except that triggerHandler() does not trigger the default behavior of the event.
$(document). ready(function(){ $('#countrylist'). change(function(e){ // Your event handler }); // And now fire change event when the DOM is ready $('#countrylist'). trigger('change'); });
Change event is fired when the value is changed by users interaction on page and not when value is modified using code.
Here you need to use .change()
or .trigger("change")
after changing the property:
$('input[type="checkbox"][name="something"]').prop("checked", false).change();
Working Demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With