I have a dynamic CheckBoxList that creates an HTML table. I’ve listed one row to keep it simple.
<table id="checkBoxList1" border="0">
<tr>
<td><span someValue="neededValue"><input id="checkBoxList1_0" type="checkbox" name="checkBoxList1$0" checked="checked" /><label for="checkBoxList1_0">TestTopic1</label></span></td>
</tr>
</table>
I also have an ASP button that will run javascript when clicked. I need the function to return the "neededValue". Here is the ASP button and the javascript function:
<asp:Button ID="btnSubscribe" runat="server" Text="Update Subscriptions" OnClientClick="return GetSelectedItem()"/>
<script type="text/javascript">
function GetSelectedItem() {
var CHK = document.getElementById("<%=checkBoxList1.ClientID%>");
var checkbox = CHK.getElementsByTagName("input");
var spans = CHK.getElementsByTagName("span");
for (var i = 0; i < checkbox.length; i++) {
if (checkbox[i].checked) {
alert("Selected = " + spans[i]);
}
}
return false;
}
</script>
Right now it returns a null value.
Replace
spans[i]withcheckbox[i].parentNode.getAttribute('someValue').I recommend using jQuery. It simplifies your code a lot, for example: