473,563 Members | 2,363 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 ?

Jul 23 '05 #1
6 5441
tr*********@gma il.com wrote:
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 ?


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

Jul 23 '05 #2
Thank you very much.

Jul 23 '05 #3
RobB wrote:
tr*********@gma il.com wrote:
I have a radio button group in a form called shippinglocatio n.
"window.form1 .shippinglocati on.value" returns "undefined" . How do I


get
the value ?

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


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
Jul 23 '05 #4
Ivo
"RobG" wrote
tr*********@gma il.com wrote:

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


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(r grp){
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(r grp)
{
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(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;
}
getRadioValue(d ocument.form1.s hippinglocation );


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(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:
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(r grp){
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
8474
by: bbxrider | last post by:
i have 2 radio buttons 1 for yes and 1 for no, it is mandatory for one to be checked, and am purposely leaving both blank on the page when i tested for .value, when neither was checked, an alert message comes back with literally 'undetermined', and i get the message 2x, once for each button alert("empty prevVolunteer is " + e.value); i.e,...
1
25628
by: Bill | last post by:
Hmmm... the onChange handler for radio buttons only seems to work when I click on the specific radio button. If I cause it to change - become unchecked - by clciking on another radio button in the group the handler doesn't get called. Is this the way it's supposed to work? I want to set optional properties of certain questions depending upon...
2
7727
by: Paul Thompson | last post by:
I am trying to use various javascript tools, all of which work in NN, to work in IE. Here's my latest annoyance. I get a list of all objects on a page using docContents = document.getElementsByTagName("*"); for (var i=0; i < docContents.length; i++) { if (docContents.type == 'text' ||
6
11176
by: juli | last post by:
Hello, I need a control which will contain radio buttons that will be added in a loop. I am using this control a source of some other control. I tried to use group box windows control and to add the radio buttons this way(in a loop): this.groupBox1.Controls.Add(radio); but I can see there only one radio button . How can I see all of them...
24
4491
by: John Gabriel | last post by:
CreateDialogIndirect() fires my Radio button click function without the radio button having being clicked. I am using VC++ 2003.NET. I believe this is yet another Microsoft bug. Have checked dialog resources and memory - both appear to be in order. Can anyone comment on this? Should CreateDialogIndirect() which is located in dlgcore.cpp ever...
22
7933
by: Saul | last post by:
I have a set of radio buttons that are created dynamically, after rendered I try loop thru this set by getting the length of the set, but I keep getting an error stating the element is undefined. I am using getElelementsByName since these are radio buttons, but it seems that the dynamic element is not seen!!! This is my code... please let...
4
2507
by: Blasting Cap | last post by:
I have a page that has a number of radio buttons that will be displayed to different access levels of a user who logs in to my website. For instance, if there are a dozen buttons, user1 will see all twelve. User2 would see buttons 1-3,10-12 and NOT any others, and so on. Since there are several buttons on the page (each one would run a...
2
5655
by: iwasjoeking | last post by:
Hi all, I am trying to make it so that the style "RowSelected" (embedded below) is applied to the table row from which the radio button is selected, and then removed when a different radio button is clicked. Right now, the below code kinda works. The javascript was copied from an example on a website from which I forgot the URL. It only...
0
2284
by: jehugaleahsa | last post by:
Hello: I have radio buttons bound to boolean properties in a business object. private void bindRadioButton(RadioButton button, string propertyName) { Binding binding = button.DataBindings.Add("Checked", exclusionBindingSource, propertyName,
1
7451
by: castron | last post by:
Hi all, I'm reading user information from Active Directory. I'm allowing the users to search by username from a web form. There will be times when the result will be more than one record. What I'm trying to do is to generate radio buttons at run-time, so users can select the user account they are looking for, and then populate the information...
0
7658
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7876
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8101
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7630
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6235
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5479
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
1
2077
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.