add dynamic checkbox in checkboxlist using javascript not working in firefox [Resolved]

Posted by Modit under ASP.NET on 2/15/2012 | Points: 10 | Views : 9824 | Status : [Member] | Replies : 3
hi i am using a code thru which on a button click event a user is able to add values in a checkboxlist using javascript.

<asp:CheckBoxList id="CheckBoxList1" runat="server">
<asp:listitem Value="1">Item 1</asp:listitem>
</asp:CheckBoxList>
<input type="button" onclick="addToCheckBoxListControl('Item 2', '2');" value="Add To CheckBoxList" />

<script type="text/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);
}
// -->
</script>


this is working fine in IE but is not working in firefox. Any idea why this is not working.
thanks




Responses

Posted by: Blessyjees on: 2/16/2012 [Member] Bronze | Points: 50

Up
0
Down

Resolved
Hi,

Update your js function with below and try again

 function addToCheckBoxListControl(textValue, valueValue) {


var tableRef = document.getElementById('<%= CheckBoxList1.ClientID %>');
var tableRow = tableRef.insertRow(1);
var tableCell = tableRow.insertCell(0);
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);
}


Blessy Baby
Digitalmesh Softech pvt Ltd
https://blessybaby.wordpress.com/

Modit, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: SureshMongam on: 2/16/2012 [Member] Starter | Points: 25

Up
0
Down
Try this code

<input type="button" onclick="addToCheckBoxListControl('textValue', 'valueValue');" value="Add To CheckBoxList" />


<script type="text/javascript">

<!--

function addToCheckBoxListControl(textValue, valueValue) {

var tableRef = document.getElementById('<%= CheckBoxList1.ClientID %>');
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 = "Item" + textValue;
checkBoxRef.value = valueValue;
tableCell.appendChild(checkBoxRef);

tableCell.appendChild(labelRef);
}


Modit, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Modit on: 2/16/2012 [Member] Starter | Points: 25

Up
0
Down
thank you for the quick response , i have one more query, i have another button from which i want to clear all items in checkboxlist using javascript.

Modit, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response