3

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.

3
  • how many elements are there in checkList1. i thing will only be one Commented Jan 15, 2014 at 9:32
  • there may be many elements checkList1 and if i select multiple items of checkboxlist then it shows multiple times alert message "on" instead of ID Commented Jan 15, 2014 at 9:35
  • try this : stackoverflow.com/questions/18924147/… Commented Jan 15, 2014 at 9:40

3 Answers 3

16

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> 
Sign up to request clarification or add additional context in comments.

1 Comment

it is returning selected items text but i want its value
1

Try debugging

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

Check to see id console.log() gives you any information about the object by pressing F12 on console window. Install firebug plugin for Firefox.

Comments

0

May this code help you:

function CheckBoxCheckOrNot(jobskill) {
    var c = document.getElementById(jobskill).getElementsByTagName('input');
    for (var i = 0; i < c.length; i++) {
        if (c[i].type == 'checkbox') {
            if (c[i].checked) {
                alert('checkbox checked');
            }
            else {
                alert('checkbox unchecked');
            }
        }
    }
}

note: jobskill is a container id which contain all check boxes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.