How to get the checkboxlist text using Javacsript in asp.net


Hi

So many time we will get scenario to read the checkbox list using JavaScript, we can do this task like this

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default3.aspx.vb" Inherits="Test_Default3" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">

function getCheckBoxListItemsChecked(elementId) {
var elementRef = document.getElementById(elementId);
var checkBoxArray = elementRef.getElementsByTagName('input');
var checkedValues = '';

for (var i = 0; i < checkBoxArray.length; i++) {
var checkBoxRef = checkBoxArray[i];

if (checkBoxRef.checked == true) {
// for getting the value
var value1 = checkBoxArray[i].value;
alert(value1)
// For getting the text value

var labelArray = checkBoxRef.parentNode.getElementsByTagName('label');
if (labelArray.length > 0) {
if (checkedValues.length > 0)
checkedValues += ', ';
checkedValues += labelArray[0].innerHTML;

}
}
}

return checkedValues;
}

function readCheckBoxList() {
var checkedItems = getCheckBoxListItemsChecked('<%= CheckBoxList2.ClientID %>');
alert(checkedItems);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:CheckBoxList ID="CheckBoxList2"  runat="server">
<asp:ListItem>C1</asp:ListItem>
<asp:ListItem>C2</asp:ListItem>
</asp:CheckBoxList>
</div>
<input type="button" onclick="readCheckBoxList();" value="Read CheckBoxList" />

</form>
</body>
</html>

Advertisements

4 Responses to “How to get the checkboxlist text using Javacsript in asp.net”

  1. Free Instagram Followers Says:

    Excellent post. Keep posting such kind of information on your site.
    Im really impressed by it.
    Hello there, You’ve performed a fantastic job. I’ll certainly digg it and for my part recommend to my friends.
    I’m sure they’ll be benefited from this web site.

  2. buy 1000 instagram followers cheap Says:

    Hi there! Do you use Twitter? I’d like to follow you if that would be ok.
    I’m definitely enjoying your blog and look forward to new posts.

    • Chandra Dev Says:

      Thank you. I don’t use Twitter.

  3. where can i buy phen375 Says:

    Hi there! Do you know if they make any plugins to assist with Search Engine
    Optimization? I’m trying to get my blog to rank for some targeted keywords but I’m
    not seeing very good success. If you know of any please share.
    Many thanks!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: