Note that there are some explanatory texts on larger screens.

plurals
  1. POhow do you select all checkboxes when user clicks a button
    primarykey
    data
    text
    <p>i have a form with a "Select All" button and a bunch of checkboxes. i want to have it select all checkboxes when the user clicks the "Select All" button.</p> <p>is there elegant way in jquery to do this?</p> <p>EDIT #2: i have isolated the problem down to this code; style="display: none;". if i remove this code it works fine. any ideas?</p> <p>EDIT: The answers below work in my test form but the button in this case is inside of a form that is inside of a div that only shows up as part of a simpledialog.show(). in this case, for some reason when i click on the button i dont see anything happen:</p> <p>javascript:</p> <pre><code>&lt;script type="text/javascript"&gt; $(document).ready(function() { $('#sdHc3').simpleDialog({ showCloseLabel: false, open: function() { $('#checkboxStatus').html(''); }, close: function() { var c = []; $('#checkboxForm :checkbox:checked').each(function() { c.push($(this).val()); }); $('#checkboxStatus').html('&amp;nbsp;&amp;nbsp;Checked &lt;b&gt;' + c.join(', ') + '&lt;/b&gt;.').show(); } }); }); </code></pre> <p> </p> <pre><code>&lt;script type="text/javascript"&gt; $(function() { $('#selectAll').click(function() { var select_all = (this.value === 'Select All'); $(':checkbox').attr('checked', select_all); this.value = (select_all) ? 'Deselect All' : 'Select All'; }); }); </code></pre> <p></p> <p>body:</p> <pre><code>&lt;div style="display: none;" class="scrollableDiv" id="simpleDialog3"&gt; &lt;h3&gt;DEMO3&lt;/h3&gt; &lt;form id="checkboxForm"&gt; &lt;input type="button" id="selectAll" value="TT" /&gt; &lt;input type="checkbox" class="chckbx" value="1" /&gt;&lt;br /&gt; &lt;input type="checkbox" class="chckbx" value="1" /&gt;&lt;br /&gt; &lt;/form&gt; &lt;p&gt; &lt;a href="#" class="close"&gt;Close&lt;/a&gt;&lt;/p&gt; &lt;/div&gt; </code></pre> <p>any idea why it wouldn't work in this case?</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.
    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