Note that there are some explanatory texts on larger screens.

plurals
  1. POjavascript checkall/uncheckall checkbox for loop
    text
    copied!<p>I have a webpage to display multiple checkboxes for each loop values. I did the below code for that purpose. It is not working well.</p> <pre><code>&lt;? $idArray=array(); foreach($this-&gt;login as $login): $idArray[]=$login['id']; endforeach; $count=count($idArray); ?&gt; </code></pre> <p>JavaScript Function</p> <pre><code>&lt;script type="text/javascript" language="javascript"&gt; var jArray = &lt;?= json_encode($idArray); ?&gt; ; var l = &lt;?= $count; ?&gt; ; for (var i = 0; i &lt; l; i++) { var value = jArray[i]; function 'setCheckboxes' + value(act) { var e = document.getElementsByClassName('names' + value); var elts_cnt = (typeof(e.length) != 'undefined') ? e.length : 0; if (!elts_cnt) { return; } for (var i = 0; i &lt; elts_cnt; i++) { e[i].checked = (act == 1 || act == 0) ? act : (e[i].checked ? 0 : 1); } } } &lt;/script&gt; </code></pre> <p>Form</p> <pre><code>&lt;? foreach($this-&gt;login as $login): ?&gt; &lt;form action="/site/select-tags" method="post" name="myform"&gt; &lt;table class="list"&gt; &lt;thead&gt; &lt;tr&gt; &lt;th&gt;Username: &lt;?=$login['username'];?&gt; &lt;/th&gt; &lt;th&gt;Password: &lt;?=$login['decryptPassword'];?&gt;&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td&gt; &lt;a href="javascript:setCheckboxes&lt;?=$login['id'];?&gt;(1);"&gt;Check All&lt;/a&gt; | &lt;a href="javascript:setCheckboxes&lt;?=$login['id'];?&gt;(0);"&gt;Uncheck All&lt;/a&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr &gt; &lt;? $count = 0; foreach($this-&gt;alltags as $all): $count++; ?&gt; &lt;td style="background-color:#F0F0F0;"&gt; &lt;input type="checkbox" name="comp[]" value="&lt;?=$all['id'];?&gt;" class="names&lt;?=$login['id'];?&gt;"&gt; &lt;label for="&lt;?=$all['tag'];?&gt;"&gt;&lt;?=$all['tag'];?&gt;&lt;/label&gt; &lt;/td&gt; &lt;?php if($count % 2 == 0 || $count ==count($this-&gt;alltags)): ?&gt; &lt;/tr&gt; &lt;?php if($count != count($this-&gt;alltags)) : ?&gt; &lt;tr&gt; &lt;?php endif; ?&gt; &lt;?php endif; ?&gt; &lt;?endforeach; ?&gt; &lt;tr&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;input type="submit" value="Add" style="background:#016ABC;color: #fff;border: 1px solid #eee;border-radius: 20px;box-shadow: 5px 5px 5px #eee;"/&gt;&lt;/td&gt; &lt;td&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;/form&gt; &lt;? endforeach; ?&gt; </code></pre> <p>I have added the function name as <strong>function 'setCheckboxes'+value(act)</strong> . But it is not the correct syntax I know. How can I use the variable name in javascript function. Or is there any better way to select checkboxes in the specified table?</p> <p>Thanks!</p>
 

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