Click here to Skip to main content
15,888,239 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have a array with key value pair, I have to iterate through the array and add list items to check list box using javascript...

How can I achieve this...?

any suggestion is appreciated.


My Code:

C#
var tableRef = document.getElementById("ContentPlaceHolder1_chklstProvider");
        var checkBoxArray = tableRef.getgetElementsByTagName('input');
        for (var rowIndex = 1; rowIndex < arr.length; rowIndex++)
        {
          var tmparr = arr[rowIndex - 1].split('^');
//            var lastrow = tableRef.rows.length;
//            var tableRow = tableRef.insertRow(1);
//            var tableCell = tableRow.insertCell(0);
//            var checkBoxRef = document.createElement('input');
//            var labelRef = document.createElement('label');
//            checkBoxRef.type = 'checkbox';

//            textValue = lastrow + 1;
//            valueValue = lastrow + 1;
//            labelRef.innerHTML = tmparr[1];
//            checkBoxRef.value = tmparr[0];
//            tableCell.appendChild(checkBoxRef);
            //            tableCell.appendChild(labelRef);
            var option = document.createElement('input');
            var label = document.createElement('label');


            option.type = 'checkbox';
            label.innerHTML = tmparr[1];
            option.value = tmparr[0];


            document.getElementById("ContentPlaceHolder1_chklstProvider").appendChild(option);
            document.getElementById("ContentPlaceHolder1_chklstProvider").appendChild(label);



commented code is 1st approach, the other is another approach..

I failed to get the checkbox list Id first of all,
It's been null or undefined....


please help me
Posted
Comments
Ed Nutting 1-Nov-12 8:42am    
Some HTML perhaps to complement your JavaScript sample? If you are failing to get the element by Id, then you simply have the wrong Id in your JavaScript (or your HTML depending on which way you look at it...). Please post your HTML so we can actually start to solve your issue. Check that you have actually set an Id on your check box and check that he Id in JavaScript is the same as in HTML. Also, make sure this JavaScript code isn't running before the Page Load event. I.e. place your code in a method that gets called on page load or at some later time/event but not beforehand because otherwise the DOM hasn't loaded and thus you can't get ANY element by any method: Id, tag name or otherwise...

Hope this helps,
Ed
[no name] 2-Nov-12 0:05am    
This is my HTML(asp tags)

<asp:CheckBoxList ID="chklstProvider" runat="server" Width="250px" CssClass="CheckBoxList">

1 solution

Source[^]
JavaScript
function addToCheckBoxListControl(textValue, valueValue)
{
 var tableRef = document.getElementById('<%= CheckBoxList1.ClientID %>');

 var tableRow = tableRef.insertRow();
 var tableCell = tableRow.insertCell();

 var checkBoxRef = document.createElement('input');
 var labelRef = document.createElement('label');

 checkBoxRef.type = 'checkbox';
 labelRef.innerHTML = textValue;
 checkBoxRef.value = valueValue;

 tableCell.appendChild(checkBoxRef);
 tableCell.appendChild(labelRef);
}
 
Share this answer
 
Comments
[no name] 2-Nov-12 0:12am    
I tried and tried, I didn't find the valid chklstProvider(in my case) object , its always null, either in chklstProvider.ClientID or just like ContentPlaceHolder1_chklstProvider
<asp:CheckBoxList ID="chklstProvider" runat="server" Width="250px" CssClass="CheckBoxList">
What's wrong with my code...?
[no name] 2-Nov-12 0:26am    
Finally Its working , on providing atleast one list item in the html , I made it work fine, but the checkboxes are not aliging vertically even if I specified the propery

asp:CheckBoxList ID="chklstProvider" runat="server" Width="250px" CssClass="CheckBoxList" RepeatDirection="Vertical">
<asp:ListItem Text="" Value="Temp">
[no name] 2-Nov-12 0:27am    
asp:CheckBoxList ID="chklstProvider" runat="server" Width="250px" CssClass="CheckBoxList" RepeatDirection="Vertical">
<asp:ListItem Text="" Value="Temp">

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900