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.
<?
$idArray=array();
foreach($this->login as $login):
$idArray[]=$login['id'];
endforeach;
$count=count($idArray);
?>
JavaScript Function
<script type="text/javascript" language="javascript">
var jArray = <?= json_encode($idArray); ?> ;
var l = <?= $count; ?> ;
for (var i = 0; i < 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 < elts_cnt; i++) {
e[i].checked = (act == 1 || act == 0) ? act : (e[i].checked ? 0 : 1);
}
}
}
</script>
Form
<?
foreach($this->login as $login):
?>
<form action="/site/select-tags" method="post" name="myform">
<table class="list">
<thead>
<tr>
<th>Username: <?=$login['username'];?> </th>
<th>Password: <?=$login['decryptPassword'];?></th>
</tr>
</thead>
<tbody>
<tr>
<td>
<a href="javascript:setCheckboxes<?=$login['id'];?>(1);">Check All</a> |
<a href="javascript:setCheckboxes<?=$login['id'];?>(0);">Uncheck All</a>
</td>
</tr>
<tr >
<?
$count = 0;
foreach($this->alltags as $all):
$count++;
?>
<td style="background-color:#F0F0F0;">
<input type="checkbox" name="comp[]" value="<?=$all['id'];?>" class="names<?=$login['id'];?>">
<label for="<?=$all['tag'];?>"><?=$all['tag'];?></label>
</td>
<?php
if($count % 2 == 0 || $count ==count($this->alltags)):
?>
</tr>
<?php if($count != count($this->alltags)) : ?>
<tr>
<?php endif; ?>
<?php endif; ?>
<?endforeach; ?>
<tr></tr>
<tr>
<td><input type="submit" value="Add" style="background:#016ABC;color: #fff;border: 1px solid #eee;border-radius: 20px;box-shadow: 5px 5px 5px #eee;"/></td>
<td></td>
</tr>
</tbody>
</table>
</form>
<?
endforeach;
?>
I have added the function name as function 'setCheckboxes'+value(act) . 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?
Thanks!