In ASP.NET, a CheckboxList control renders as follows:
---
<table id="Table1" border="0">
<tr>
<td><input id="cbl1_0" type="checkbox" name="cbl1$0" /><label
for="cbl1_0">Item One</label></td>
</tr>
<tr>
<td><input id="cbl1_1" type="checkbox" name="cbl1$1" /><label
for="cbl1_1">Item Two</label></td>
</tr>
...
</table>
---
It does not render a "value" attribute, so the only way to retrieve
the text is to query the innerText of the label at the same index. The
following script would be a working sample:
---
function checkSpecific(cbl, text)
{
var tblChkBoxList = document.getElementById(cbl);
var chkBoxList = tblChkBoxList.getElementsByTagName("input");
var lblList = tblChkBoxList.getElementsByTagName("label");
for (i = 0; i < chkBoxList.length; i++)
{
var currChkBox = chkBoxList[i];
var currLabelText = lblList[i].innerText;
if (currLabelText.lastIndexOf(text) > -1)
currChkBox.checked = true;
}
}
---
Note that the code works only in IE... other browsers do not support
the "innerText" property (Use "textContent", instead).
On Aug 21, 10:41 pm, Ana <[email protected]> wrote:
> Hi,
>
> I need a version in javascript for *checkBoxList.Items(x).Text* to
> check if the item x ends with an specific word (given as parameter in
> the function) and, if so, check the item.
> What I'm trying to do is a kind of Select All/None, but in this case I
> have to select only the items that ends with an specific word.
> Currently my function is like this:
>
> function checkSpecific(cbl, text) {
> var chkBoxList = document.getElementById(cbl);
> var chkBoxCount = chkBoxList.getElementsByTagName
> ("input");
> for (i = 0; i < chkBoxCount.length; i++) {
> var currentItem = chkBoxCount[i]
> var expectedIndex = currentItem.count - text.length
> if (currentItem.lastIndexOf(text) == expectedIndex) {
> chkBoxCount[i].checked = true;
> }
> }
> }
>
> Any ideas about how I can do it?
>
> Thanks in advance,
>
> Ana