Monday, 5 August 2013

Get multiple values from checkboxlist selected values in textbox as comma seperated using javascript in asp.net

Java Script Funtion:

function GetSelectedValue() {
        var chkBox = document.getElementById("<%=chkList.ClientID%>");
        var checkbox = chkBox.getElementsByTagName("input");
        var objTextBox = document.getElementById("<%=txtChkValue.ClientID%>");
        var counter = 0;
        objTextBox.value = "";
        for (var i = 0; i < checkbox.length; i++) {
            if (checkbox[i].checked) {
                var chkBoxText = checkbox[i].parentNode.getElementsByTagName('label');
                if (objTextBox.value == "") {
                    objTextBox.value = chkBoxText[0].innerHTML;
                }
                else {
                    objTextBox.value = objTextBox.value + ", " + chkBoxText[0].innerHTML;
                }
            }
        }

    }

where chkList is CheckboxList and txtChkValue is Textbox as given below:

<asp:TextBox ID="txtChkValue1" runat="server" CssClass="textbox"
                Width="270px" Height="25px"></asp:TextBox>
             
                <asp:CheckBoxList ID="chkList1" runat="server" onclick="GetSelectedValue1();">
                </asp:CheckBoxList>