Monday, May 20, 2024
Homepage · c#
 Popular · Latest · Hot · Upcoming
52
rated 0 times [  57] [ 5]  / answers: 1 / hits: 43002  / 11 Years ago, wed, january 15, 2014, 12:00:00

I am working on an asp.net project in which i have a checkboxlist which i have bound using



DataTable dt = new Process_Hotels().SelectAllFacilty();           
if (dt.Rows.Count > 0)
{
cblHotelFacility.DataSource = dt;
cblHotelFacility.DataTextField = Facility;
cblHotelFacility.DataValueField = ID;
cblHotelFacility.DataBind();

foreach (ListItem li in cblHotelFacility.Items)
{
li.Attributes.Add(JSvalue, li.Value);
}
}


and now i want to get selected value ID of checkboxlist using javascript on button click.For that i have following javascript code on button click:



<script type=text/javascript>                                                                                   

function test() {
var checkList1 = document.getElementById('<%= cblHotelFacility.ClientID %>');
var checkBoxList1 = checkList1.getElementsByTagName(input);
var checkBoxSelectedItems1 = new Array();

for (var i = 0; i < checkBoxList1.length; i++) {
if (checkBoxList1[i].checked) {
checkBoxSelectedItems1.push(checkBoxList1[i].value);
//alert('checked:' + checkBoxSelectedItems1.push(checkBoxList1[i].getAttribute(JSvalue)).value);
alert('checked - : ' + checkBoxList1[i].value)
}
}
}
</script>


but the on clicking button the selected checkboxlist is showing 0. I want to get ID of selected checkboxlist items.Please help.


More From » c#

 Answers
29

Try this :



<script type = text/javascript>

function GetCheckBoxListValues(chkBoxID)
{
var chkBox = document.getElementById('<%= cblHotelFacility.ClientID %>');
var options = chkBox.getElementsByTagName('input');
var listOfSpans = chkBox.getElementsByTagName('span');
for (var i = 0; i < options.length; i++)
{
if(options[i].checked)
{
alert(listOfSpans[i].attributes[JSvalue].value);
}
}
}


</script>

[#73159] Tuesday, January 14, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
vaughns

Total Points: 20
Total Questions: 112
Total Answers: 112

Location: Falkland Islands
Member since Mon, Jul 13, 2020
4 Years ago
;