There is no such thing for checkbox as "value" property. Actually there is but usually is not used .What do you exactly mean? You want get its state (property "checked" - e.g. document.getElementById('checkboxId').checked)
or get its label (text displayed next to checkbox) then you'll need to find label whose "for" attribute is set to id of checkbox e.g. <label for="checkboxId">This is chb label</label>
I think what you want is to use a javascript method to access the value of each list item within the checkbox list. I am in the same situation as you. You would think we can just do .value since in the html code, you do have a value - I have not yet found
a feasible solution for myself but I did run across this:
What they did here in their example, is they went ahead and added each list item's value in the "alt" tag suring databound of the checkbox list and then in javascript they called each child node and referenced the "alt" tag versus the "value" tag that we
can't seem to access through javascript.
If anyone has a solution, please feel free to chime in! [:)]
My example code of how I actually loop through the checkbox list to find a list item that is selected is this:
var cblBoardCom = document.getElementById(form + "cblBoardCom");
var len = cblBoardCom.getElementsByTagName(
'input').length;
for (i = 0; i < len; i++) {
var opt = document.getElementById(cblBoardCom.id +
"_" + i);
if (opt.checked ==
false) {opt.checked =
true;
}
The reason why I am using so much vaiables is that my Javascript is actually very lengthy and as Ajax uses the $get function I am using variables. [;)]
"If you have knowledge, let others light their candles in it."
— Margaret Fuller
As has been already posted, there is no client-side "value" property in a CheckBoxList. That is a server-side only property. You can only get the Text property of the ListItem (Item 1, Item 2, etc in this example).
<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 )
{
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AFAIK, you cannot get the value property of a ListItem in a CheckBoxList.
// You can only get the Text property, which will be in an HTML label element.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var labelArray = checkBoxRef.parentNode.getElementsByTagName('label');
if ( labelArray.length > 0 )
{
if ( checkedValues.length > 0 )
checkedValues += ', ';
I am getting undefined if checkboxlist checked is used.
Specify how can i get the value from checkbox list
Use the code posted in my previous post. It shows how to do that. A CheckBoxList renders as an HTML Table tag and does not have checked property. The child controls are CheckBoxes and do have the checked property.
I can't beleave how horribly wrong some of the replies here are. CheckboxList does not correctly renders it's Listitems, in DropdownList the ListItems Value property is properly rendered as client side value property of the option element.
bab_ganesh
Member
127 Points
64 Posts
How to get the CheckBoxlist Value using Javascript?
Aug 16, 2007 08:20 AM|LINK
Hi,
I am using checkboxlist in asp.net 2.0 C# and I have a masterpage.
I bind a list of records in cheklistbox from db.
I could not get the value from javascript.
i tried. document.getElementById("ctl00_ContentPlaceHolder1_ChkItem_0").value
but it returns "on". I could not the value.
How to get the value.? please help me.
eseidel
Participant
1607 Points
505 Posts
Re: How to get the CheckBoxlist Value using Javascript?
Aug 16, 2007 12:27 PM|LINK
Try .checked instead of .value. Will return true or false.
Eric
RSSole
Member
204 Points
63 Posts
Re: How to get the CheckBoxlist Value using Javascript?
Aug 16, 2007 12:28 PM|LINK
There is no such thing for checkbox as "value" property. Actually there is but usually is not used .What do you exactly mean? You want get its state (property "checked" - e.g. document.getElementById('checkboxId').checked)
or get its label (text displayed next to checkbox) then you'll need to find label whose "for" attribute is set to id of checkbox e.g. <label for="checkboxId">This is chb label</label>
mvang
Participant
1708 Points
361 Posts
Re: How to get the CheckBoxlist Value using Javascript?
Nov 18, 2008 05:22 PM|LINK
I think what you want is to use a javascript method to access the value of each list item within the checkbox list. I am in the same situation as you. You would think we can just do .value since in the html code, you do have a value - I have not yet found a feasible solution for myself but I did run across this:
http://hspinfo.wordpress.com/2008/08/14/get-checkboxlist-values-using-javascript/
What they did here in their example, is they went ahead and added each list item's value in the "alt" tag suring databound of the checkbox list and then in javascript they called each child node and referenced the "alt" tag versus the "value" tag that we can't seem to access through javascript.
If anyone has a solution, please feel free to chime in! [:)]
My example code of how I actually loop through the checkbox list to find a list item that is selected is this:
var cblBoardCom = document.getElementById(form + "cblBoardCom");var len = cblBoardCom.getElementsByTagName(
'input').length; for (i = 0; i < len; i++) { var opt = document.getElementById(cblBoardCom.id + "_" + i); if (opt.checked == false) {opt.checked = true;}
The reason why I am using so much vaiables is that my Javascript is actually very lengthy and as Ajax uses the $get function I am using variables. [;)]
— Margaret Fuller
NC01
All-Star
82577 Points
15430 Posts
MVP
Re: How to get the CheckBoxlist Value using Javascript?
Nov 18, 2008 05:44 PM|LINK
As has been already posted, there is no client-side "value" property in a CheckBoxList. That is a server-side only property. You can only get the Text property of the ListItem (Item 1, Item 2, etc in this example).
<form id="form1" runat="server">
<asp:CheckBoxList id="CheckBoxList1" runat="server">
<asp:listitem Value="1">Item 1</asp:listitem>
<asp:listitem Value="2">Item 2</asp:listitem>
<asp:listitem Value="3">Item 3</asp:listitem>
</asp:CheckBoxList>
<input type="button" onclick="readCheckBoxList();" value="Read CheckBoxList" />
</form>
<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 )
{
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AFAIK, you cannot get the value property of a ListItem in a CheckBoxList.
// You can only get the Text property, which will be in an HTML label element.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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('<%= CheckBoxList1.ClientID %>');
alert('Items checked: ' + checkedItems);
}
// -->
</script>
NC...
ishwaryasrao
Member
8 Points
5 Posts
Re: How to get the CheckBoxlist Value using Javascript?
May 06, 2009 07:34 AM|LINK
I am getting undefined if checkboxlist checked is used.
Specify how can i get the value from checkbox list
NC01
All-Star
82577 Points
15430 Posts
MVP
Re: How to get the CheckBoxlist Value using Javascript?
May 06, 2009 11:20 AM|LINK
Use the code posted in my previous post. It shows how to do that. A CheckBoxList renders as an HTML Table tag and does not have checked property. The child controls are CheckBoxes and do have the checked property.
NC...
Eswar.Palach...
Member
46 Points
90 Posts
Re: How to get the CheckBoxlist Value using Javascript?
May 12, 2010 08:13 AM|LINK
Thank you
Hyderabad India.
amsterdamhar...
Member
2 Points
1 Post
Re: How to get the CheckBoxlist Value using Javascript?
Jan 30, 2013 12:19 AM|LINK
I can't beleave how horribly wrong some of the replies here are. CheckboxList does not correctly renders it's Listitems, in DropdownList the ListItems Value property is properly rendered as client side value property of the option element.
CheckboxList does not do this but checkbox definitely has a value property . The CheckboxList just screws it up giving it a value of "on" or "off".
Work around for this shortcomming has already been given by using the databound event to add properties to the ListItems:
for (int index = 0; index < chklistTeams.Items.Count; index++){ ListItem item = chklistTeams.Items[index]; string valueToSet = chklistTeams.Items[index].Value; item.Attributes.Add("JSvalue", valueToSet);//set client side attribute JSvalue }