Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set $.prop() without triggering change

Tags:

jquery

<li>
 <input type="checkbox" id="master" />
   <ul>
   <li>
     <input type="checkbox" />

$('input').bind('change', function() {}); // exists elsewhere, cannot change

When I click the top level input, I want to set .prop('checked', true) for all of its children, but I want to avoid triggering the change event for each child checkbox.

How could I set .prop('checked', true) to an input without triggering change?

Edit: (expanding)

$('#master').click(function() {
    $(this).parent().find('input').each(function(n, in) {
        $(in).prop('checked', true); // this triggers a change event that I need to stop
    });
});
like image 962
tester Avatar asked Feb 03 '12 21:02

tester


2 Answers

try to unbind the onchange listener, then bind it again once the property has been set.

like image 148
Drew Dahlman Avatar answered Oct 02 '22 02:10

Drew Dahlman


What you are describing is the default functionality. Changing an element from javascript does not fire the change event.

http://jsfiddle.net/8JsP4/

Notice how if you click on a checkbox in the example, you get an alert, but if you click the button, no alert.

like image 45
James Montagne Avatar answered Oct 02 '22 00:10

James Montagne