Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Getting the Values of CheckBox(es) Selected

Reply
Thread Tools

Getting the Values of CheckBox(es) Selected

 
 
rishabhshrivastava@gmail.com
Guest
Posts: n/a
 
      07-23-2006
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;
}

 
Reply With Quote
 
 
 
 
Janwillem Borleffs
Guest
Posts: n/a
 
      07-23-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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


 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      07-23-2006

(E-Mail Removed) 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

 
Reply With Quote
 
Danny
Guest
Posts: n/a
 
      07-23-2006

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

Danny
 
Reply With Quote
 
rishabhshrivastava@gmail.com
Guest
Posts: n/a
 
      07-23-2006
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:
> (E-Mail Removed) 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


 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      07-23-2006

(E-Mail Removed) 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting selected values from gridview (checkbox) and inserting the Id in the database stavo23 ASP .Net 0 02-26-2008 03:26 PM
DropDownList 2 always returns Selected = 0 for all items - even selected item Iain ASP .Net 3 12-11-2006 11:07 AM
How to/Help: Getting Values for Selected Rows in a Repeater renil ASP .Net 3 02-06-2006 06:30 PM
Getting CheckBoxList selected values Patrick Olurotimi Ige ASP .Net 1 06-16-2005 08:52 AM
Getting selected text values =?Utf-8?B?SGFzc2FuIEJhc3NhbQ==?= ASP .Net 5 02-28-2005 03:27 PM



Advertisments