Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing .prop using jQuery does not trigger .change event

Tags:

jquery

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?

like image 387
fuzzybabybunny Avatar asked Jun 25 '14 13:06

fuzzybabybunny


People also ask

Does jQuery Val trigger change event?

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.

What is trigger change in jQuery?

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.

How do I create a trigger drop change event?

$(document). ready(function(){ $('#countrylist'). change(function(e){ // Your event handler }); // And now fire change event when the DOM is ready $('#countrylist'). trigger('change'); });


1 Answers

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

like image 194
Milind Anantwar Avatar answered Sep 21 '22 04:09

Milind Anantwar