469,336 Members | 5,552 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,336 developers. It's quick & easy.

Radio Button Properties

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

Jul 23 '05 #1
6 5302
tr*********@gmail.com wrote:
I have a radio button group in a form called shippinglocation.
"window.form1.shippinglocation.value" returns "undefined". How do I get the value ?


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

getRadioValue(document.form1.shippinglocation);

Jul 23 '05 #2
Thank you very much.

Jul 23 '05 #3
RobB wrote:
tr*********@gmail.com wrote:
I have a radio button group in a form called shippinglocation.
"window.form1.shippinglocation.value" returns "undefined". How do I


get
the value ?

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

getRadioValue(document.form1.shippinglocation);


To me, the logic is clearer when written:

function getRadioValue(rgrp){
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(rgrp){
for (var i = rgrp.length - 1; i >= 0; i-- )
if (rgrp[i].checked) return rgrp[i].value;
return '';
}

Dang, a smorgasbord. :-)
--
Rob
Jul 23 '05 #4
Ivo
"RobG" wrote
tr*********@gmail.com wrote:

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

getRadioValue(document.form1.shippinglocation);
To me, the logic is clearer when written:

function getRadioValue(rgrp){
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)


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
use:

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

Dang, a smorgasbord. :-)
--
Rob


Jul 23 '05 #5
Ivo wrote:
[...]

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).
Ooops, good point.

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


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

Jul 23 '05 #6
RobG wrote:
RobB wrote: <snip>
function getRadioValue(rgrp)
{
var i = rgrp.length;
do
if (rgrp[--i].checked)
return rgrp[i].value;
while (i);
return '';
}
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(rgrp){
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;
}
getRadioValue(document.form1.shippinglocation);


To me, the logic is clearer when written:


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.
function getRadioValue(rgrp){
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:
The numeric values that type-convert to boolean false are zero and NaN,
all others (including all negative numbers) type-convert to boolean
true.
function getRadioValue(rgrp){
for (var i = rgrp.length - 1; i >= 0; i-- )

<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.
Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Paul Thompson | last post: by
6 posts views Thread by juli | last post: by
4 posts views Thread by Blasting Cap | last post: by
reply views Thread by jehugaleahsa | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.