Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First of all you should wrap your checkboxes with <code>&lt;label&gt;</code> so that when the label is clicked the checkbox is also toggled (user friendly).</p> <p>Then i recommend using lists to group your checkboxes. (you can style them with css in any you want).</p> <p>Something like this:</p> <pre><code>&lt;ul&gt; &lt;li class="entityCheckboxHeader1" &gt; &lt;label&gt;&lt;input type="checkbox" id="entityCheckboxHeader1"&gt;Company&lt;/label&gt; &lt;ul&gt; &lt;li&gt;&lt;label&gt;&lt;input id="modalEntity11" class="entity1" type="checkbox" name="company[]"&gt;Microsoft&lt;/label&gt;&lt;/li&gt; &lt;li&gt;&lt;label&gt;&lt;input id="modalEntity12" class="entity2" type="checkbox" name="company[]"&gt;Apple&lt;/label&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/li&gt; &lt;li class="entityCheckboxHeader2"&gt; &lt;label&gt;&lt;input type="checkbox" id="entityCheckboxHeader2"&gt;Country&lt;/label&gt; &lt;ul&gt; &lt;li&gt;&lt;label&gt;&lt;input id="modalEntity21" class="entity3" type="checkbox" name="country[]"&gt;USA&lt;/label&gt;&lt;/li&gt; &lt;li&gt;&lt;label&gt;&lt;input id="modalEntity22" class="entity4" type="checkbox" name="country[]"&gt;UK​​​​​​​​​​​​​​​​​​​​​​​​​​​​​&lt;/label&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/li&gt; &lt;/ul&gt;​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ </code></pre> <p>Now you can use this very simply jquery to fullfill your task:</p> <pre><code>$("li :checkbox").change(function(){ $(this).closest("li").find("ul :checkbox").prop("checked", $(this).is(":checked")); });​ </code></pre> <p>​ <a href="http://jsfiddle.net/eKTAk/3/" rel="nofollow">http://jsfiddle.net/eKTAk/3/</a></p> <p>Note that I have changed the names of the fields so that you can read them like this in php a script (post form):</p> <pre><code>$companies = $_POST['companies']; //Gets the array of values for the checked companies $countries = $_POST['countries']; //Gets the array of values for the checked companies </code></pre>
 

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