Getting the Values of CheckBox(es) Selected

Discussion in 'Javascript' started by rishabhshrivastava@gmail.com, Jul 23, 2006.

  1. Guest

    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;
    }
    , Jul 23, 2006
    #1
    1. Advertising

  2. wrote:
    > 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
    Janwillem Borleffs, Jul 23, 2006
    #2
    1. Advertising

  3. RobG Guest

    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.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;


    > }


    --
    Rob
    RobG, Jul 23, 2006
    #3
  4. Danny Guest

    Danny, Jul 23, 2006
    #4
  5. Guest

    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:
    > 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.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;
    >
    >
    > > }

    >
    > --
    > Rob
    , Jul 23, 2006
    #5
  6. RobG Guest

    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.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.


    --
    Rob
    RobG, Jul 24, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joel Reinford
    Replies:
    1
    Views:
    638
    Curt_C [MVP]
    Jan 13, 2004
  2. =?Utf-8?B?SGFzc2FuIEJhc3NhbQ==?=

    Getting selected text values

    =?Utf-8?B?SGFzc2FuIEJhc3NhbQ==?=, Feb 28, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    4,033
    =?Utf-8?B?SGFzc2FuIEJhc3NhbQ==?=
    Feb 28, 2005
  3. Kirk
    Replies:
    2
    Views:
    1,726
  4. stavo23
    Replies:
    0
    Views:
    2,160
    stavo23
    Feb 26, 2008
  5. Kirk
    Replies:
    0
    Views:
    186
Loading...

Share This Page