Unable to access CheckedListBox from javascript

Dec 27, 2008 at 8:36 AM
Hello all fellows...

I have used the CheckedListBox web component in one of my aspx pages. I have successfully binded data from my database to the listbox. But the problem arised when I tried to select all the entries that have been checked/selected from the control itself using my client-side javascript code. My CheckedListBox control's ID is "lstemployee" and here is my javascript code :-

My intension is to select all entries from the list which is selected/checked and put them along with their values into a textbox and I should use client-side code for this. So javascript is the best alternative I think....

<script type="text/javascript" language="javascript">
   
function collect()
    {
      var l=document.getElementById("lstemployee");
      var t=document.getElementById("TextBox1");
     
      var s="";

      for(var i=0;i<l.length;i++)
      {
        if(l.options[i].selected==true)
        {
            s=s + "," + (l.options[i].value + " - " + l.options[i].text);   
         }
      }
     
      if(s!="")
      {
        s=s.substr(1);
      }
    
      t.value=s.Trim();
     
      return false;
    }
   
</script>

I have fired this js code from a button server control and I used the below code in order to attach this code to it:-

protected void Page_Load(object sender, EventArgs e)
{

   Button2.Attributes.Add("onclick", "javascript:return collect()");
}

The problem is mainly
--> the js code can't finding the element "lstemployee"

Then I commented the entire code and just fire an alert in order to see total item count of the listbox using the following code :-

alert(document.getElementById("lstemployee").length);

the output was :- an alert with "undefined" message.

Then I used the following :-

document.getElementByTagName("mb:CheckedListBox ");
alert(document.getElementById("lstemployee").length);


This time the alert displayed me 0 although there were 86 records in the listbox.
I just can't figure out what's the wrong stuff that I'm doing....It's really sticking my head off...Can anybody here show me the correct direction....It's really urgent....If I can't figure this out then I have to manually code my own control which is too time consuming and I really hate to do that...

Looking forward to get some positive replies...
Thanks for any appreciation....in advance

regards
Shouvik