471,616 Members | 1,901 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,616 software developers and data experts.

Getting the Values of CheckBox(es) Selected

Hey All,
Can someone suggest me a way to get the values of CheckBox(es) selected
in a CheckBoxList control using JAVASCRIPT.
I am pasting my current code gere but its not working need some
suggestions pls.

function CheckBoxList(cntrlName)
{
var cntrlValue = listValuesCheckBox(cntrlName);
return cntrlValue ;
}
function listValuesCheckBox(objectName)
{
var list = "";
for (var i=0; i<objectName.length;i++){
if (objectName.Options[i].selected = true){
list += objectName.Options[i].value+ '~';
}
}
return list;
}

Jul 23 '06 #1
6 11749
ri****************@gmail.com wrote:
if (objectName.Options[i].selected = true){
This is an assignment, and therefore, will always evaluate to TRUE.

Use one of the following forms instead:

if (objectName.Options[i].selected == true){

if (objectName.Options[i].selected){
JW
Jul 23 '06 #2

ri****************@gmail.com wrote:
Hey All,
Can someone suggest me a way to get the values of CheckBox(es) selected
in a CheckBoxList control using JAVASCRIPT.
I am pasting my current code gere but its not working need some
suggestions pls.

function CheckBoxList(cntrlName)
{
var cntrlValue = listValuesCheckBox(cntrlName);
return cntrlValue ;
}
function listValuesCheckBox(objectName)
{
var list = "";
for (var i=0; i<objectName.length;i++){
if (objectName.Options[i].selected = true){
Careful with case, a select element has an options collection, not an
Options collection.

list += objectName.Options[i].value+ '~';
}
}
return list;
Presuming that objectName is a reference to a select element, there is
no need to loop through all the options. The select element's
selectedIndex property will tell you which one is selected. Replace
the entre function with:

return '~' + objectName.options[objectName.selectedIndex].value;
Modern browsers will be happy with:

return '~' + objectName.value;

}
--
Rob

Jul 23 '06 #3

Check at http://www.javascriptkit.com/javatut...diocheck.shtml on how to get the .checked
checkboxes :) .

Danny
Jul 23 '06 #4
Hey Rob,
When I replaced it with the line you told me JavaScript thows the Error
Below:-

Microsoft JScript runtime error: 'options' is null or not an object

Please Advise.

Thanks
RobG wrote:
ri****************@gmail.com wrote:
Hey All,
Can someone suggest me a way to get the values of CheckBox(es) selected
in a CheckBoxList control using JAVASCRIPT.
I am pasting my current code gere but its not working need some
suggestions pls.

function CheckBoxList(cntrlName)
{
var cntrlValue = listValuesCheckBox(cntrlName);
return cntrlValue ;
}
function listValuesCheckBox(objectName)
{
var list = "";
for (var i=0; i<objectName.length;i++){
if (objectName.Options[i].selected = true){

Careful with case, a select element has an options collection, not an
Options collection.

list += objectName.Options[i].value+ '~';
}
}
return list;

Presuming that objectName is a reference to a select element, there is
no need to loop through all the options. The select element's
selectedIndex property will tell you which one is selected. Replace
the entre function with:

return '~' + objectName.options[objectName.selectedIndex].value;
Modern browsers will be happy with:

return '~' + objectName.value;

}

--
Rob
Jul 23 '06 #5
ri****************@gmail.com wrote in news:1153687584.732898.55360
@i42g2000cwa.googlegroups.com:
Can someone suggest me a way to get the values of CheckBox(es) selected
in a CheckBoxList control using JAVASCRIPT.
Hi,

Note thad an option in a select control has the * selected * property.

Checkboxes and radio buttons have the * checked * property.
Jul 23 '06 #6

ri****************@gmail.com wrote:
Hey Rob,
Please don't top post, reply below trimmed quotes.
When I replaced it with the line you told me JavaScript thows the Error
Below:-

Microsoft JScript runtime error: 'options' is null or not an object
Probably because I expected you to be using a select element rather
than a set of checkboxes. Your original post was somewhat confusing,
here is an example of getting the selected checkboxes from a set:

<script type="text/javascript">

function listValuesCheckBox(checkBoxRef)
{
var list = [];
for (var i=0, len=checkBoxRef.length; i<len; i++){
if (checkBoxRef[i].checked){
list.push(checkBoxRef[i].value);
}
}
alert(list.join('\n'));
}

</script>

<form action="">
<label for="cb01"><input type="checkbox" id="cb01"
name="cbSet1" value="cb 01">cb 01</label><br>
<label for="cb02"><input type="checkbox" id="cb02"
name="cbSet1" value="cb 02">cb 02</label><br>
<label for="cb03"><input type="checkbox" id="cb03"
name="cbSet1" value="cb 03">cb 03</label><br>
<input type="button" value="Show checked"
onclick="listValuesCheckBox(this.form.cbSet1);">
<input type="reset">
</form>
If you are trying to create a delimited list, a joined array is simpler
(and much faster in IE) than concatenating strings using the "+="
compound operator.
--
Rob

Jul 23 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Tripharn Teki | last post: by
5 posts views Thread by Hassan Bassam | last post: by
2 posts views Thread by rishabhshrivastava | last post: by
1 post views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.