Getting the Values of CheckBox(es) Selected

R

rishabhshrivastava

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.selected = true){
list += objectName.Options.value+ '~';
}
}
return list;
}
 
J

Janwillem Borleffs

if (objectName.Options.selected = true){


This is an assignment, and therefore, will always evaluate to TRUE.

Use one of the following forms instead:

if (objectName.Options.selected == true){

if (objectName.Options.selected){


JW
 
R

RobG

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.selected = true){


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

list += objectName.Options.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;
 
R

rishabhshrivastava

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

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.selected = true){


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

list += objectName.Options.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;

 
R

RobG


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.checked){
list.push(checkBoxRef.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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top