Note that there are some explanatory texts on larger screens.

plurals
  1. POOptimizing jQuery code
    primarykey
    data
    text
    <p>I have a table that lists locations and then allows a user to select 1 or more permissions for that location. I have the following code working that I can select the top row check-box and you can check all the boxes in that column. I'm looking for tips to optimize what I'm doing. You click on the ".all###" check-box and all the ones with a class of ".XXX" get checked. How can I optimize my jquery? I'm still learning and although I got it to work im sure its not the best route. Any help would be appreciated. </p> <p>This is my part of my HTML code</p> <pre><code> &lt;table&gt; &lt;colgroup&gt;&lt;/colgroup&gt; &lt;colgroup&gt;&lt;/colgroup&gt; &lt;colgroup&gt;&lt;/colgroup&gt; &lt;colgroup span="5"&gt;&lt;/colgroup&gt; &lt;thead&gt; &lt;tr&gt; &lt;th class="locationCode"&gt;Location Code&lt;/th&gt; &lt;th class="locationName"&gt;Name&lt;/th&gt; &lt;th class="locationAddress"&gt;Address&lt;/th&gt; &lt;th class="selectOption"&gt;Admin&lt;/th&gt; &lt;th class="selectOption"&gt;Remote&lt;/th&gt; &lt;th class="selectOption"&gt;Support&lt;/th&gt; &lt;th class="selectOption"&gt;Misc&lt;/th&gt; &lt;th class="selectOption"&gt;Logging&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tfoot&gt; &lt;tr&gt; &lt;td&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/tfoot&gt; &lt;tbody&gt; &lt;tr&gt; &lt;th colspan="3" class="grayBackground"&gt;&lt;/th&gt; &lt;td class="center"&gt;&lt;input type="checkbox" class="allAdmin admin"&gt;&lt;/td&gt; &lt;td class="center"&gt;&lt;input type="checkbox" class="allRemote remote"&gt;&lt;/td&gt; &lt;td class="center"&gt;&lt;input type="checkbox" class="allSupport support"&gt;&lt;/td&gt; &lt;td class="center"&gt;&lt;input type="checkbox" class="allMisc misc"&gt;&lt;/td&gt; &lt;td class="center"&gt;&lt;input type="checkbox" class="allLogging logging"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;VST&lt;/td&gt; &lt;td&gt;Demo #1&lt;/td&gt; &lt;td&gt;1 Street, City State&lt;/td&gt; &lt;td class="center"&gt;&lt;input type="checkbox" class="admin"&gt;&lt;/td&gt; &lt;td class="center"&gt;&lt;input type="checkbox" class="remote"&gt;&lt;/td&gt; &lt;td class="center"&gt;&lt;input type="checkbox" class="support"&gt;&lt;/td&gt; &lt;td class="center"&gt;&lt;input type="checkbox" class="misc"&gt;&lt;/td&gt; &lt;td class="center"&gt;&lt;input type="checkbox" class="logging"&gt;&lt;/td&gt; &lt;/tr&gt; </code></pre> <p>and my jQuery</p> <pre><code> $(function() { $(".allAdmin").change(function() { if ($(this).prop("checked")) { $(".admin").prop("checked", true); return; } $(".admin").prop("checked", false); }); $(".allRemote").change(function() { if ($(this).prop("checked")) { $(".remote").prop("checked", true); return; } $(".remote").prop("checked", false); }); $(".allSupport").change(function() { if ($(this).prop("checked")) { $(".support").prop("checked", true); return; } $(".support").prop("checked", false); }); $(".allMisc").change(function() { if ($(this).prop("checked")) { $(".misc").prop("checked", true); return; } $(".misc").prop("checked", false); }); $(".allLogging").change(function() { if ($(this).prop("checked")) { $(".logging").prop("checked", true); return; } $(".logging").prop("checked", false); }); }); </code></pre>
    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.
 

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