Need help accessing radio button in function ...

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

    #1

    Need help accessing radio button in function ...

    I have a function that changes which radio button is selected. I need to pass the form name to the
    function but not the field name and am doing so like:

    changeRadio(thi s.form);

    The function:

    changeRadio(for mname){

    // I have skipped how the variable fieldname is created all that is important is that it needs to be
    created in the function, not passed to it, thus the need (I think) for the following

    var radio2check = eval(formname+' .'+fieldname); // Don't work
    var radio2check = eval("document. actualformname. "+fieldname ); // Works

    // later in the function the above is used like:

    radio2check[0].checked = true;

    }

    So in other words, when I manually enter the form name in the eval part it works fine but when I use
    the formname containing this.form passed into the function it don't work. I need to know how to
    access the radio button by combining the formname passed into the function and the fieldname
    variable created in the function.


  • J Mox

    #2
    Re: Need help accessing radio button in function ...

    I tried changing

    var radio2check = eval(formname+' .'+fieldname);

    to

    var radio2check = eval('document. '+formname.name +'.'+fieldname) ;

    and it appears to work. If anyone has a better solution, please let me know.





    Comment

    • RobG

      #3
      Re: Need help accessing radio button in function ...

      J Mox wrote:[color=blue]
      > I tried changing
      >
      > var radio2check = eval(formname+' .'+fieldname);
      >
      > to
      >
      > var radio2check = eval('document. '+formname.name +'.'+fieldname) ;[/color]

      Eval is almost never, ever needed - follow the link from the FAQ on
      square bracket notation:

      <URL:http://www.jibbering.c om/faq/faq_notes/square_brackets .html>


      A guess at what you might need is:

      var radio2check = document.forms[formname].elements[fieldname];

      [color=blue]
      >
      > and it appears to work. If anyone has a better solution, please let me know.[/color]

      Your eval method may work, but it is less than optimal.


      --
      Rob

      Comment

      • Richard Cornford

        #4
        Re: Need help accessing radio button in function ...

        J Mox wrote:[color=blue]
        > I tried changing
        >
        > var radio2check = eval(formname+' .'+fieldname);
        >
        > to
        >
        > var radio2check = eval('document. '+formname.name +'.'+fieldname) ;
        >
        > and it appears to work. If anyone has a better solution,
        > please let me know.[/color]

        If the latter "works" then the variable (paramerter?) - formname - is
        not a string representation of a name but an object that has a property
        called - name -. It is also an object with the property - name - where
        that name happens to be the name of a FORM Element object. That is
        unlikely to be a coincidence and so I would deduce that - formname - is
        a reference to an object that is a FORM element object, indeed it is
        _the_ form element object. So some confusion has been introduced by
        giving it a name that actually conceals its real nature.

        If you do - eval('document. '+formRef.name) ; - what you get back is -
        formRef -, a reference to the FORM object that you started with, so you
        can skip that. if you do - eval('fromRef.' +fieldName) - you are doing
        the same as - formRef[fieldname] - but slower and more indirectly.

        var radio2check = formname[fieldname];

        - should "work" at least as effectively as the - eval - and is; shorter,
        simpler, faster and more direct (and so easier to debug and maintain).
        (but do change the - formname - variable name)

        A general rule for newcomers to javascript would be that if you are
        considering using - eval - then you have the opportunity to learn
        something new that will not use - eval - and be objectively better than
        the - eval - use.

        Richard.


        Comment

        • J Mox

          #5
          Re: Need help accessing radio button in function ...

          "RobG" <rgqld@iinet.ne t.au> wrote in message
          news:436cb793$0 $1699$5a62ac22@ per-qv1-newsreader-01.iinet.net.au ...[color=blue]
          >J Mox wrote:[color=green]
          >> I tried changing
          >>
          >> var radio2check = eval(formname+' .'+fieldname);
          >>
          >> to
          >>
          >> var radio2check = eval('document. '+formname.name +'.'+fieldname) ;[/color]
          >
          > Eval is almost never, ever needed - follow the link from the FAQ on square bracket notation:
          >
          > <URL:http://www.jibbering.c om/faq/faq_notes/square_brackets .html>
          >
          >
          > A guess at what you might need is:
          >
          > var radio2check = document.forms[formname].elements[fieldname];
          >
          >[color=green]
          >>
          >> and it appears to work. If anyone has a better solution, please let me know.[/color]
          >
          > Your eval method may work, but it is less than optimal.
          >
          >
          > --
          > Rob[/color]

          Thanks for the link. If I was passing the actual form name as a string, which is what I mistakenly
          thought I was in effect doing when I passed this.form to the function, then I think your solution
          would work but since I am passing what I have now come to understand is a object referencing a form
          the following works.

          var radio2check = formname[fieldname];


          Comment

          • J Mox

            #6
            Re: Need help accessing radio button in function ...

            "Richard Cornford" <Richard@litote s.demon.co.uk> wrote in message
            news:dkig88$fbq $1$830fa17d@new s.demon.co.uk.. .[color=blue]
            >J Mox wrote:[color=green]
            >> I tried changing
            >>
            >> var radio2check = eval(formname+' .'+fieldname);
            >>
            >> to
            >>
            >> var radio2check = eval('document. '+formname.name +'.'+fieldname) ;
            >>
            >> and it appears to work. If anyone has a better solution,
            >> please let me know.[/color]
            >
            > If the latter "works" then the variable (paramerter?) - formname - is
            > not a string representation of a name but an object that has a property
            > called - name -. It is also an object with the property - name - where
            > that name happens to be the name of a FORM Element object. That is
            > unlikely to be a coincidence and so I would deduce that - formname - is
            > a reference to an object that is a FORM element object, indeed it is
            > _the_ form element object. So some confusion has been introduced by
            > giving it a name that actually conceals its real nature.
            >
            > If you do - eval('document. '+formRef.name) ; - what you get back is -
            > formRef -, a reference to the FORM object that you started with, so you
            > can skip that. if you do - eval('fromRef.' +fieldName) - you are doing
            > the same as - formRef[fieldname] - but slower and more indirectly.
            >
            > var radio2check = formname[fieldname];
            >
            > - should "work" at least as effectively as the - eval - and is; shorter,
            > simpler, faster and more direct (and so easier to debug and maintain).
            > (but do change the - formname - variable name)
            >
            > A general rule for newcomers to javascript would be that if you are
            > considering using - eval - then you have the opportunity to learn
            > something new that will not use - eval - and be objectively better than
            > the - eval - use.
            >
            > Richard.
            >
            >[/color]

            Thanks, you helped me understand the difference between objects and properties. I was passing
            this.form to the function as formname (I now see and agree how the variable was confusingly named)
            mistakenly thinking that it resulted in the actual form name being passed to the function. I think I
            now understand that passing this.form to a function passes a form element object from which form
            properties can be accessed such as your simpler example which worked fine.


            Comment

            • RobG

              #7
              Re: Need help accessing radio button in function ...

              J Mox wrote:
              [...][color=blue]
              >
              > Thanks for the link. If I was passing the actual form name as a string, which is what I mistakenly
              > thought I was in effect doing when I passed this.form to the function, then I think your solution
              > would work but since I am passing what I have now come to understand is a object referencing a form
              > the following works.
              >
              > var radio2check = formname[fieldname];[/color]

              You are passing a reference to the form object.

              Glad to help. :-)


              --
              Rob

              Comment

              Working...