Radio Button Properties

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • trevordixon@gmail.com

    #1

    Radio Button Properties

    I have a radio button group in a form called shippinglocatio n.
    "window.form1.s hippinglocation .value" returns "undefined" . How do I get
    the value ?

  • RobB

    #2
    Re: Radio Button Properties

    trevordixon@gma il.com wrote:[color=blue]
    > I have a radio button group in a form called shippinglocatio n.
    > "window.form1.s hippinglocation .value" returns "undefined" . How do I[/color]
    get[color=blue]
    > the value ?[/color]

    function getRadioValue(r grp)
    {
    var i = rgrp.length;
    do
    if (rgrp[--i].checked)
    return rgrp[i].value;
    while (i);
    return '';
    }

    getRadioValue(d ocument.form1.s hippinglocation );

    Comment

    • trevordixon@gmail.com

      #3
      Re: Radio Button Properties

      Thank you very much.

      Comment

      • RobG

        #4
        Re: Radio Button Properties

        RobB wrote:[color=blue]
        > trevordixon@gma il.com wrote:
        >[color=green]
        >>I have a radio button group in a form called shippinglocatio n.
        >>"window.form1 .shippinglocati on.value" returns "undefined" . How do I[/color]
        >
        > get
        >[color=green]
        >>the value ?[/color]
        >
        >
        > function getRadioValue(r grp)
        > {
        > var i = rgrp.length;
        > do
        > if (rgrp[--i].checked)
        > return rgrp[i].value;
        > while (i);
        > return '';
        > }
        >
        > getRadioValue(d ocument.form1.s hippinglocation );
        >[/color]

        To me, the logic is clearer when written:

        function getRadioValue(r grp){
        var i = rgrp.length;
        while ( i-- )
        if (rgrp[i].checked) return rgrp[i].value;
        return '';
        }

        For those who don't like the type conversion (when i-- gets to
        -1 it evaluates to false) use:

        function getRadioValue(r grp){
        for (var i = rgrp.length - 1; i >= 0; i-- )
        if (rgrp[i].checked) return rgrp[i].value;
        return '';
        }

        Dang, a smorgasbord. :-)


        --
        Rob

        Comment

        • Ivo

          #5
          Re: Radio Button Properties

          "RobG" wrote[color=blue]
          > trevordixon@gma il.com wrote:[color=green]
          > >
          > > function getRadioValue(r grp)
          > > {
          > > var i = rgrp.length;
          > > do
          > > if (rgrp[--i].checked)
          > > return rgrp[i].value;
          > > while (i);
          > > return '';
          > > }
          > >
          > > getRadioValue(d ocument.form1.s hippinglocation );[/color]
          >
          > To me, the logic is clearer when written:
          >
          > function getRadioValue(r grp){
          > var i = rgrp.length;
          > while ( i-- )
          > if (rgrp[i].checked) return rgrp[i].value;
          > return '';
          > }
          >
          > For those who don't like the type conversion (when i-- gets to
          > -1 it evaluates to false)[/color]

          Does it? Try this:

          var s = -1;
          alert( new Boolean(s) ); // true !

          Besides, the while loop never gets to -1 because it ends when i becomes 0
          (which does evaluate to false).

          The logic may not be immediately clear, but do() loops are by far the
          fastest loops that javascript has to offer. Next come while() loops, and
          for() loops, when compared, are really just *slow*. (It may not make much
          difference when dealing with only a few radio buttons.)
          For a comparison, see
          < http://www.websiteoptimization.com/speed/10/10-2.html >
          although there appears to a bug in the code on that page. They also explain
          the unbeatable loops of Duff's Device.
          A working testpage is at
          < http://4umi.com/web/javascript/optimize.htm >
          --
          Ivo
          [color=blue]
          > use:
          >
          > function getRadioValue(r grp){
          > for (var i = rgrp.length - 1; i >= 0; i-- )
          > if (rgrp[i].checked) return rgrp[i].value;
          > return '';
          > }
          >
          > Dang, a smorgasbord. :-)
          >
          >
          > --
          > Rob[/color]





          Comment

          • RobG

            #6
            Re: Radio Button Properties

            Ivo wrote:
            [...][color=blue]
            >
            > Does it? Try this:
            >
            > var s = -1;
            > alert( new Boolean(s) ); // true !
            >
            > Besides, the while loop never gets to -1 because it ends when i becomes 0
            > (which does evaluate to false).[/color]

            Ooops, good point.
            [color=blue]
            >
            > The logic may not be immediately clear, but do() loops are by far the
            > fastest loops that javascript has to offer. Next come while() loops, and
            > for() loops, when compared, are really just *slow*. (It may not make much
            > difference when dealing with only a few radio buttons.)[/color]

            The point was not speed of execution, it was how easy the code is
            to read and maintain. I was just posing my point of view, not
            taking exception to RobB's (excellent) code.
            [color=blue]
            > For a comparison, see
            > < http://www.websiteoptimization.com/speed/10/10-2.html >
            > although there appears to a bug in the code on that page. They also explain[/color]

            Yes, there is a form with a name "testLoop" and a function called
            "testLoop".

            The difference between 'for' and 'do/while' on my ancient laptop
            was about 3% (861 vs 831) but yes, do/while was faster. It was
            revealing to see that simply reversing the direction of count -
            down rather than up - produces a huge performance gain (861 to
            381 - a 55% reduction).

            Reverse 2 was faster than reverse 3.

            So I guess wherever performance is an issue, reversed do/while's
            the go. :-x

            --
            Rob

            Comment

            • Richard Cornford

              #7
              Re: Radio Button Properties

              RobG wrote:[color=blue]
              > RobB wrote:[/color]
              <snip>[color=blue][color=green]
              >> function getRadioValue(r grp)
              >> {
              >> var i = rgrp.length;
              >> do
              >> if (rgrp[--i].checked)
              >> return rgrp[i].value;
              >> while (i);
              >> return '';
              >> }[/color][/color]

              In the event that - i - was zero at the start of the loop (unlikely as
              an empty set of buttons should not result in any collection at all; -
              rgrp - would be undefined) then the loop would go on for ever (but
              actually error-out when - rgrp[-n] - resolved to undefined and so could
              not be type-converted into an object for the property accessor -
              rgrp[-n].checked -). A more cautious version might go:-

              function getRadioValue(r grp){
              var i = (rgrp && rgrp.length);
              if(i--){ //post decrement on undefined == NaN
              do{
              if(rgrp[i].checked){
              return rgrp[i].value;
              }
              }while (--i); //pre-decrement is faster than post
              }
              return null;
              }
              [color=blue][color=green]
              >> getRadioValue(d ocument.form1.s hippinglocation );[/color]
              >
              > To me, the logic is clearer when written:[/color]

              However, it is often argued that the statements used with for - while,
              do-while, if, etc, should be exclusively Block statements, with even
              single statements contained within those block statements. This is for
              clarity and to avoid errors occurring when the code is modified by the
              addition of statements. I witnessed a very experienced programmer trip
              over this very consideration just last week; adding an extra statement
              that was intended to be conditional on the preceding - if - statement
              but would actually have always been executed. Douglas Crokford's JSLINT
              will not even allow these structures without the Block statement.
              [color=blue]
              > function getRadioValue(r grp){
              > var i = rgrp.length;
              > while ( i-- )
              > if (rgrp[i].checked) return rgrp[i].value;
              > return '';
              > }
              >
              > For those who don't like the type conversion (when i-- gets to
              > -1 it evaluates to false) use:[/color]

              The numeric values that type-convert to boolean false are zero and NaN,
              all others (including all negative numbers) type-convert to boolean
              true.
              [color=blue]
              > function getRadioValue(r grp){
              > for (var i = rgrp.length - 1; i >= 0; i-- )[/color]
              <snip>

              For a count-down - for - loop (where the - length - can be assumed to be
              non-negative) I like:-

              for( var c = x.length; c-- ; ){
              ...
              }

              - avoiding the initial subtraction and the comparison operation on each
              loop.

              Richard.


              Comment

              Working...