Pass radiobutton control array to function?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Seeker

    #1

    Pass radiobutton control array to function?

    Newbie question here...

    I have a form with some radio buttons. To verify that at least one of the
    buttons was chosen I use the following code ("f" is my form object) :

    var btnChosen;
    for (count = 0; count <= 1; count++) {
    if (eval(f.RadioBu ttons[count].checked)) {
    btnChosen = true;
    }
    }
    if (!btnChosen) { /* Code here to warn no button selected */}

    ....I'd like to move this to a function. Can I pass the "f.RadionButton s"
    array to a function in Javascript? This way I could simply say:

    if (! fncSelected(f.R adioButtons)) { /* Code here to warn no button selected
    */}


  • Michael Winter

    #2
    Re: Pass radiobutton control array to function?

    On Mon, 01 Mar 2004 23:49:34 GMT, Seeker <dont.spam@me.h ere> wrote:
    [color=blue]
    > I have a form with some radio buttons. To verify that at least one of the
    > buttons was chosen I use the following code ("f" is my form object) :
    >
    > var btnChosen;
    > for (count = 0; count <= 1; count++) {[/color]

    This declares 'count' to be a global variable. I very much doubt you want
    that. Use 'var': for (var count = 0;....

    I also doubt that you only want to check the first radio button.

    ...; count < f.RadioButtons. length; ...

    would be more appropriate.
    [color=blue]
    > if (eval(f.RadioBu ttons[count].checked)) {[/color]

    Why are you using eval() here?

    if( f.RadioButtons[ count ].checked ) {

    is sufficient. There will very rarely be any need for you to use eval().
    If you think you need it, you probably don't understand the language well
    enough. That's not meant to be derogatory: the better alternatives can be
    a little obscure, but they will be better nevertheless.

    There are previous threads on the topic of proper eval() usage. Use the
    Google Groups archive if you're interested.
    [color=blue]
    > btnChosen = true;
    > }
    > }
    > if (!btnChosen) { /* Code here to warn no button selected */}
    >
    > ...I'd like to move this to a function. Can I pass the "f.RadionButton s"
    > array to a function in Javascript? [...][/color]

    'f.RadioButtons ' will actually be a collection[1], but in short: yes, you
    can. Below is a more efficient version of your original snippet.

    function isChecked( group ) {
    var size = group.length;

    for( var i = 0; i < size; ++i ) {
    if( group[ i ].checked ) return true;
    }
    return false;
    }

    function validate( form ) {
    alert( 'Button checked: ' + isChecked( form.RadioButto ns ));
    }
    ...
    <button type="button" onclick="valida te(this.form);" >

    Hope that helps,
    Mike


    [1] Collections can be thought of as simplified versions of arrays. You
    can index the elements, and determine the size (length) but, unlike
    arrays, you can't use methods such as join() and shift().

    --
    Michael Winter
    M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

    Comment

    • mscir

      #3
      Re: Pass radiobutton control array to function?

      Seeker wrote:[color=blue]
      > I have a form with some radio buttons. To verify that at least one of the
      > buttons was chosen I use the following code ("f" is my form object) :
      >
      > var btnChosen;
      > for (count = 0; count <= 1; count++) {
      > if (eval(f.RadioBu ttons[count].checked)) {
      > btnChosen = true;
      > }
      > }
      > if (!btnChosen) { /* Code here to warn no button selected */}
      >
      > ...I'd like to move this to a function. Can I pass the "f.RadionButton s"
      > array to a function in Javascript? This way I could simply say:[/color]



      Pass by Value / Pass by Reference

      When passing a single value as an argument to a functions you must refer
      to the element being passed, not the array itself. If you just use the
      array name, then what you are passing is the array, not its values.

      It is also important to understand the difference between pass by value
      and pass by reference with arrays.

      Primitive data types are passed by value. This means that when you pass
      them to a function or use them in an assignment statement, what gets
      passed is the value, not the variable itself. A copy of the value is
      made to be passed to the function or variable.

      Arrays and objects, on the other hand, are passed by reference. This
      means that when you pass them to a function or use them in an assignment
      statement, the reference to the variable is passed. No new copy is
      created. Instead, the function argument or variable is pointed to, or
      set to reference, the same location in memory. Thus, any changes you
      make affect the original.

      Array elements are passed based on the value of its contents, it is the
      array itself that is automatically passed by reference.

      .... the web page has a few simple examples of passing arrays to functions.

      Comment

      • Seeker

        #4
        Re: Pass radiobutton control array to function?


        "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message
        news:opr37ks3uo 5vklcq@news-text.blueyonder .co.uk...[color=blue]
        > On Mon, 01 Mar 2004 23:49:34 GMT, Seeker <dont.spam@me.h ere> wrote:
        >[color=green]
        > > I have a form with some radio buttons. To verify that at least one of[/color][/color]
        the[color=blue][color=green]
        > > buttons was chosen I use the following code ("f" is my form object) :
        > >
        > > var btnChosen;
        > > for (count = 0; count <= 1; count++) {[/color]
        >
        > This declares 'count' to be a global variable. I very much doubt you want
        > that. Use 'var': for (var count = 0;....
        >
        > I also doubt that you only want to check the first radio button.[/color]

        Thanks... It's not my code. There are a great many things I don't like about
        the script I'm modifying. Hopefully I can clean it up.

        [color=blue][color=green]
        > > ...I'd like to move this to a function. Can I pass the "f.RadionButton s"
        > > array to a function in Javascript? [...][/color]
        >
        > 'f.RadioButtons ' will actually be a collection[1], but in short: yes, you
        > can. Below is a more efficient version of your original snippet.[/color]

        I appreciate the help. Trying to prove myself useful to get out of a deadend
        position here.

        Thanks!


        Comment

        • Seeker

          #5
          Re: Pass radiobutton control array to function?


          "mscir" <mscir@access4l ess.net> wrote in message
          news:1047l5495c s0t71@corp.supe rnews.com...[color=blue]
          > Seeker wrote:[color=green]
          > > I have a form with some radio buttons. To verify that at least one of[/color][/color]
          the[color=blue][color=green]
          > > buttons was chosen I use the following code ("f" is my form object) :
          > >
          > > var btnChosen;
          > > for (count = 0; count <= 1; count++) {
          > > if (eval(f.RadioBu ttons[count].checked)) {
          > > btnChosen = true;
          > > }
          > > }
          > > if (!btnChosen) { /* Code here to warn no button selected */}
          > >
          > > ...I'd like to move this to a function. Can I pass the "f.RadionButton s"
          > > array to a function in Javascript? This way I could simply say:[/color]
          >
          >[/color]
          http://academ.hvcc.edu/~kantopet/jav...rent=js+arrays[color=blue]
          >[/color]
          <snip>
          Array elements are passed based on the value of its contents, it is the[color=blue]
          > array itself that is automatically passed by reference.
          >
          > ... the web page has a few simple examples of passing arrays to functions.[/color]

          Thanks! It's appreciated!


          Comment

          • Lee

            #6
            Re: Pass radiobutton control array to function?

            Seeker said:[color=blue]
            >
            >Newbie question here...
            >
            >I have a form with some radio buttons. To verify that at least one of the
            >buttons was chosen I use the following code ("f" is my form object) :
            >
            > var btnChosen;
            > for (count = 0; count <= 1; count++) {
            > if (eval(f.RadioBu ttons[count].checked)) {
            > btnChosen = true;
            > }
            > }
            > if (!btnChosen) { /* Code here to warn no button selected */}
            >
            >...I'd like to move this to a function. Can I pass the "f.RadionButton s"
            >array to a function in Javascript? This way I could simply say:
            >
            >if (! fncSelected(f.R adioButtons)) { /* Code here to warn no button selected
            >*/}[/color]

            Yes, you can do that.
            Get rid of the eval(), though. It's not doing anything.

            function fncSelected(but tons){
            for (i=0;i<buttons. length;i++){
            if(buttons[i].checked){
            return true;
            }
            }
            return false;
            }

            However, the best way to make sure that at least one radio button
            is selected is to preselect a reasonable default in the HTML.

            Comment

            Working...