0

Hi,

In my page I have a checkBoxList and I need to select all items that has an specific word. Let's suppose my CBL is:

VB.NET

C#.NET

Java2SE

Java2EE


And suppose that I need to select only the items about .NET (the items that cointain the string ".NET"). How can I check (in Javascript) if current Item in the CheckBoxList contains the string I'm passing as parameter?

Right now my code 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].innerText

          var expectedIndex = currentItem.length - text.length

          if (currentItem.lastIndexOf(text) == expectedIndex) {

              chkBoxCount[i].checked = true;

          }

    }

}

But the problem is that I'm not able to get the text of the currentItem (VB.NET, C#.NET, etc). Does anyone know how can I do it?

Thanks in advance,


Ana

2
Contributors
3
Replies
7
Views
8 Years
Discussion Span
Last Post by brigzy
0

I found the solution in this forum: http://forums.asp.net/p/1439302/3361665.aspx#3361665

In my case, the solution is:

function checkSpecific(cbl, text) {
            var chkBoxList = document.getElementById(cbl);
            var chkBoxCount = chkBoxList.getElementsByTagName('input');

            for (var i = 0; i < chkBoxCount.length; i++) {
                var elementRef = chkBoxCount[i];
                var labelArray = elementRef.parentNode.getElementsByTagName('label');
                var currentItem = labelArray[0].innerHTML;
                var expectedIndex = currentItem.length - text.length;
                if ((elementRef.type == 'checkbox') && (currentItem.lastIndexOf(text) == expectedIndex)) {
                    if (labelArray.length > 0) {
                        chkBoxCount[i].checked = true;
                    }

                } else {
                chkBoxCount[i].checked = false;
                }
            }
        }

I hope this can help someone in the future =)

0

Please can you show the OnClick call you have used, I need to understand what parameters you have passed, is it this.id?
Thanks
Richard

0

Please can you show the OnClick call you have used, I need to understand what parameters you have passed, is it this.id?
Thanks
Richard

Oh I see it must be OnClick="checkSpecific(this, 'NET').

I calling from a gridView Template checkboxlist :

function readListControl(checkBoxList)
{
    var options = checkBoxList.getElementsByTagName('input');
    var arrayOfCheckBoxLabels= checkBoxList.getElementsByTagName("label");
    for(i=0;i<=options.length;i++)
    {
        var opt = options[i];
        if(opt.checked)
        {
            Alert(arrayOfCheckBoxLabels[i].innerText); 
        } 
    }
}

and control defined as

<ItemTemplate>
        <asp:CheckBoxList ID="chkAction" runat="server" BorderColor="#006600" 
            BorderWidth="2px" style="text-align: left" 
             onc >
            <asp:ListItem onclick="readListControl(this)">ID Verification Required</asp:ListItem>
            <asp:ListItem onclick="readListControl(this)" Enabled="False">Received Proof ID</asp:ListItem>
        </asp:CheckBoxList>
    
    </ItemTemplate>

For some reason the javascript:

var options = checkBoxList.getElementsByTagName('input');

allways has Count of 0, I don't understand why.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.