Tuesday, March 25, 2008

Select or Deselect a list of checkboxes

We may have a situation where we have a number of checkboxes generated dynamically and we need to either select them ALL or deselect them all at the same time
We can handle this in javascript as shown below

function setAllChecked(che)
{
cur = document.getElementsByName("checkBoxSelectAll");
if (che.checked)
{
for (i = 0; i < cur.length; i++)
{
cur[i].checked = true;
}
}

else
{
for (i = 0; i < cur.length; i++)
{
cur[i].checked = false;
}
}
}

<table cellpadding="2" cellspacing="2">
<th>
<input type=checkbox name="allSelect" onclick="setAllChecked(this)">
Select All
</th>

<nested:iterate property="records">
<tr>
<td>
<nested:checkbox property="select" styleId="checkBoxSelectAll" />
</td>
<td class="personName">
<nested:hidden property="personName"/>
<nested:write property="personName"/>
</td>
</tr>
</nested:iterate>
</table>

No comments: