Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To work with your own code you need to understand the order of events. When you programmatically call click() on the checkbox the javascript (checkMe() for children) executes before the state of each child checkbox is changed (e.g., adding attribute 'checked'). It is because of this reason that the checkMe() function was adding and removing ids in the selected array in the reverse order. You can confirm this by adding the following debug line in the checkMe function:</p> <pre><code>console.log('Checked state of checkbox id:' + id + ' is: ' + checkbox.prop('checked')); </code></pre> <p>Case1: Clicking checkAll when it is Unchecked; it calls checkMe() for each child checkbox but finds the 'checked' attribute as undefined. So it executes the delete code. After executing checkMe the 'checked' attribute is added on the checkbox.</p> <p>Case2: Clicking checkAll when it is Checked; the checkMe() function finds the 'checked' attribute previously added and fills the array. Later an event is probably fired to remove the 'checked' attribute.</p> <p>I changed the following lines to quickly test this and seems to be working:</p> <p>Bind checkMe on change event instead of click in reload function:</p> <pre><code>$('.table_checkbox').bind('change', checkMe); </code></pre> <p>Change the condition for unchecked children in checkAll function when the .checkbox_all is checked:</p> <pre><code>if($(this).prop('checked') === false) {/*call child click*/} //Use prop instead of attr because it takes care of 'undefined' cases as well. If you want to keep using attr because you're on an older version of jquery then add something like: typeof $(this).attr('checked') == 'undefined' </code></pre> <p>and also the condition when .checkbox_all is unchecked:</p> <pre><code>if($(this).prop('checked') === true) {/*call child click*/} </code></pre> <p>Hope this helps. Here's a <a href="http://jsbin.com/upacil/1/edit" rel="nofollow">jsbin</a> to play with..</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload