Note that there are some explanatory texts on larger screens.

plurals
  1. POJquery click event with binded event
    primarykey
    data
    text
    <p>Hi guys I am having a problem with Events. I have a checkbox list and I have a main check box that checks all boxes. When I clickEvent some of my checkbox list items it should add data-id attr to the "selected obj". So in my case when I press main check box to check all others every thing is ok (it simply clicks all other elements). but when i do that it empties my array. I mean if i uncheck it will be the way it supposed to be but checked (when uncheck it fills when i check it empties).</p> <pre><code>...... var selected = {}; var reload = function(){ selected = {}; $('.checkbox_all').unbind('click'); $('.table_checkbox').unbind('click'); $('.checkbox_all').bind('click', checkAll); $('.table_checkbox').bind('click', checkMe); } var checkMe = function(e){ var checkbox = $(e.target); var id = checkbox.attr('data-id'); //console.log(id); if(checkbox.attr('checked')){ selected[id] = id; }else{ if(selected[id]) delete selected[id]; } console.log(selected); } var checkAll = function(e){ if($(e.target).attr('checked')){ $('.table_checkbox').each(function(){ if($(this).attr('checked') === false){ $(this).click(); } }); }else{ $('.table_checkbox').each(function(){ if($(this).attr('checked') === true){ $(this).click(); } }); } //console.log(selected); } ....... </code></pre> <p>HTML:</p> <pre><code> &lt;tr&gt;&lt;th class="table-header-check"&gt;&lt;input type="checkbox" class="checkbox_all"/&gt;&lt;/th&gt;&lt;/tr&gt; &lt;tr class=""&gt;&lt;td&gt;&lt;input type="checkbox" data-id="5" class="table_checkbox"&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr class="alternate-row"&gt;&lt;td&gt;&lt;input type="checkbox" data-id="6" class="table_checkbox"&lt;/td&gt;&lt;/tr&gt; &lt;tr class="alternate-row"&gt;&lt;td&gt;&lt;input type="checkbox" data-id="8" ....ETC\ </code></pre> <p>My problem is that when i click .checkbox_all it should click on all .table_checkbox(that r cheched or uncheched)... it just clicks all checkboxes like a main checkbox... it works fine, but i have an event all other checkboxes if i click em i add some data to array when i unclick em it removes data from array.... so when im clicking checkboxes sepperatly they add /remove data to array properly... but when im clicking on main checkbox... it clicks on right checkboxes but the data array is empty when all checked and full when all unchecked... it must be the opposite way</p>
    singulars
    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.
 

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