473,607 Members | 2,674 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Please tell me the generic way to check different radio button groups?

I want to check each button groups and save the checked value into a 2
dimensional array. But, it doesn't work. Could anyone tell me what's
wrong with my code. My code is as follow:

<html>
<body>
<script language="javas cript">
<!--
var temp = new Array(2)
var status = new Array();

function getRadios( paramform )
{
for( var iElement=0; iElement<paramf orm.elements.le ngth; iElement++ )
{
if( paramform.eleme nts[iElement].checked )
{
temp[0] = paramform.eleme nts[iElement].name;
temp[1] = paramform.eleme nts[iElement].value;
status[status.length] = temp;
}
}
}

<form name="training" >
<table>
<tr>
<td>Microsoft Outlook: </td>
<td>
<input type='radio' name="outlook" value="START"
onClick="getRad ios(document.tr aining)"> START
<input type='radio' name="outlook" value="COMPLETE D"
onClick="getRad ios(document.tr aining)"> COMPLETED
<input type='radio' name="outlook" value="IN PROGRESS"
onClick="getRad ios(document.tr aining)"> INPROGRESS
</td>
</tr>

<tr>
<td>Microsoft Word: </td>
<td>
<input type='radio' name="word" value="START"
onClick="getRad ios(document.tr aining)"> START
<input type='radio' name="word" value="COMPLETE D"
onClick="getRad ios(document.tr aining)"> COMPLETED
<input type='radio' name="word" value="IN PROGRESS"
onClick="getRad ios(document.tr aining)"> INPROGRESS
</td>
</tr>

</table>
</form>

</body>
</html>
Jul 23 '05 #1
15 2111
tabonni wrote:
I want to check each button groups and save the checked value into a 2
dimensional array. But, it doesn't work.
"Does not work" is a useless error description. [psf 4.11]
Could anyone tell me what's wrong with my code.
Could you tell us what you expected and what you observed?
My code is as follow:

<html>
<body>
<script language="javas cript">
<http://validator.w3.or g/>
<!--
Remove this, it is obsolete.
var temp = new Array(2)
In ECMAScript/J(ava)Script, arrays are dynamic by nature. Dimensioning
the array on initialization is not only unnecessary, but also error-prone.
Particularly a number as sole constructor argument may cause an array with
two undefined elements to be created in one implementation, and an array
with one element of type "number" with value 2 in another.
var status = new Array();

function getRadios( paramform )
{
for( var iElement=0; iElement<paramf orm.elements.le ngth; iElement++ )
{
if( paramform.eleme nts[iElement].checked )
{
temp[0] = paramform.eleme nts[iElement].name;
temp[1] = paramform.eleme nts[iElement].value;
status[status.length] = temp;
}
}
}
You look up references far too often which makes the script run slow.
Consider this:

function getRadios(f)
{
if (f && (e = f.elements))
{
for (var iElement = 0, len = e.length; iElement < len; i++)
{
var o = e[iElement];
if (o.checked)
{
status[status.length] = [o.name, o.value];
}
}
}
}

And, as you see, do not use tab chracters for indentation,
especially not when posting code.
<form name="training" >
<table>
<tr>
<td>Microsoft Outlook: </td>
Do not use tables for layout purposes alone.
<td>
<input type='radio' name="outlook" value="START"
onClick="getRad ios(document.tr aining)"> START
[...]>
</form>


No need for proprietary referencing like `document.train ing'
and no need for defining the listener for every element. Use
event bubbling instead:

function getRadios(e)
{
var t = (e && e.target) || event.srcElemen t;
if (t.tagName && t.tagName.toLow erCase() == "input"
&& t.type && t.type.toLowerC ase() == "radio")
{
if (t.checked)
{
status[status.length] = [t.name, t.value];
}
}
}

<form ... onclick="getRad ios(event)">
...
</form>

But the problem with your code and even with my code is that you always
add elements to the array, even if the required data is already added.
This looks like a problem suited more for Object objects instead of Array
objects, but why do you think you need to store the values in an array
although already stored in DOM objects in the first place?
PointedEars
Jul 23 '05 #2
tabonni wrote:
I want to check each button groups and save the checked value into a 2
dimensional array. But, it doesn't work. Could anyone tell me what's
wrong with my code. My code is as follow:

<html>
<body>
<script language="javas cript">
<!--
var temp = new Array(2)
var status = new Array();

function getRadios( paramform )
{
for( var iElement=0; iElement<paramf orm.elements.le ngth; iElement++ )
{
if( paramform.eleme nts[iElement].checked )
{
temp[0] = paramform.eleme nts[iElement].name;
temp[1] = paramform.eleme nts[iElement].value;
status[status.length] = temp;
}
}
}

Along with Thomas' observations, why not terminate the loop when you
find an element checked?

Mick
Jul 23 '05 #3
My purpose on storing each radio name and value into an array is for
me to set the cookie easier (I think).

My idea is:
I got a form with different radio button groups. After the user check
the radio values, I will store names and values into a 2D array. For
example: the user check "outlook, start" and "word, completed", I will
put it into status[0][0]=outlook, status[0][1]=start,...

Then, I can put them into cookie like: Use a for..loop and setCookie(
status[name], status[value]); And later on, I can get the data in the
cookie and display on the form again.
Cheers
Bonnie

Mick White <mw******@BOGUS rochester.rr.co m> wrote in message news:<c_******* *************@t wister.nyroc.rr .com>...
tabonni wrote:
I want to check each button groups and save the checked value into a 2
dimensional array. But, it doesn't work. Could anyone tell me what's
wrong with my code. My code is as follow:

<html>
<body>
<script language="javas cript">
<!--
var temp = new Array(2)
var status = new Array();

function getRadios( paramform )
{
for( var iElement=0; iElement<paramf orm.elements.le ngth; iElement++ )
{
if( paramform.eleme nts[iElement].checked )
{
temp[0] = paramform.eleme nts[iElement].name;
temp[1] = paramform.eleme nts[iElement].value;
status[status.length] = temp;
}
}
}

Along with Thomas' observations, why not terminate the loop when you
find an element checked?

Mick

Jul 23 '05 #4
Thomas 'PointedEars' Lahn <Po*********@nu rfuerspam.de> wrote in message news:<41******* *****@PointedEa rs.de>...
tabonni wrote:
I want to check each button groups and save the checked value into a 2
dimensional array. But, it doesn't work.


"Does not work" is a useless error description. [psf 4.11]
Could anyone tell me what's wrong with my code.


Could you tell us what you expected and what you observed?
My code is as follow:

<html>
<body>
<script language="javas cript">


<http://validator.w3.or g/>
<!--


Remove this, it is obsolete.
var temp = new Array(2)


In ECMAScript/J(ava)Script, arrays are dynamic by nature. Dimensioning
the array on initialization is not only unnecessary, but also error-prone.
Particularly a number as sole constructor argument may cause an array with
two undefined elements to be created in one implementation, and an array
with one element of type "number" with value 2 in another.
var status = new Array();

function getRadios( paramform )
{
for( var iElement=0; iElement<paramf orm.elements.le ngth; iElement++ )
{
if( paramform.eleme nts[iElement].checked )
{
temp[0] = paramform.eleme nts[iElement].name;
temp[1] = paramform.eleme nts[iElement].value;
status[status.length] = temp;
}
}
}


You look up references far too often which makes the script run slow.
Consider this:

function getRadios(f)
{
if (f && (e = f.elements))
{
for (var iElement = 0, len = e.length; iElement < len; i++)
{
var o = e[iElement];
if (o.checked)
{
status[status.length] = [o.name, o.value];
}
}
}
}

And, as you see, do not use tab chracters for indentation,
especially not when posting code.
<form name="training" >
<table>
<tr>
<td>Microsoft Outlook: </td>


Do not use tables for layout purposes alone.
<td>
<input type='radio' name="outlook" value="START"
onClick="getRad ios(document.tr aining)"> START
[...]>
</form>


No need for proprietary referencing like `document.train ing'
and no need for defining the listener for every element. Use
event bubbling instead:

function getRadios(e)
{
var t = (e && e.target) || event.srcElemen t;
if (t.tagName && t.tagName.toLow erCase() == "input"
&& t.type && t.type.toLowerC ase() == "radio")
{
if (t.checked)
{
status[status.length] = [t.name, t.value];
}
}
}

<form ... onclick="getRad ios(event)">
...
</form>

But the problem with your code and even with my code is that you always
add elements to the array, even if the required data is already added.
This looks like a problem suited more for Object objects instead of Array
objects, but why do you think you need to store the values in an array
although already stored in DOM objects in the first place?
PointedEars


maybe it could be solved by using hashes(associat ive arrays), instead of
status[status.length] = [t.name, t.value];
one could do
status[name]=value;
so that status always keeps selected values from each name group, no duplicates..
Jul 23 '05 #5
On 3 Aug 2004 17:14:19 -0700, tabonni <ta*****@yahoo. com> wrote:
My purpose on storing each radio name and value into an array is for
me to set the cookie easier (I think).

My idea is:
I got a form with different radio button groups. After the user check
the radio values, I will store names and values into a 2D array. For
example: the user check "outlook, start" and "word, completed", I will
put it into status[0][0]=outlook, status[0][1]=start,...

Then, I can put them into cookie like: Use a for..loop and setCookie(
status[name], status[value]); And later on, I can get the data in the
cookie and display on the form again.


[snip]

In that case, it might be better to change that 2D array to a single
dimensioned array and use an object to hold the name/value pairs:

function Pair(n, v) {
this.name = n;
this.value = v;
}

status[0] = new Pair('outlook', 'start');
status[1] = new Pair('word', 'completed');
// ...
for(var i = 0, n = status.length; i < n; ++i) {
setCookie(statu s[i].name, status[i].value);
}

Much cleaner and easier to understand.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail
Jul 23 '05 #6
Michael Winter wrote:
<snip>
status[0] = new Pair('outlook', 'start');

<snip>

As an observation in passing, the global variable identifier - status -
corresponds with the - status - property of the window object. As -
window.status - is one of those properties with an overloaded setter
assigning an Array to the property might just have the Array
type-converted to a string and the result displayed in the window's
status bar. And attempts to read the property might be returning a
string primitive, subsequently type-converted to a String object when
used with bracket notation property accessors, allowing assignment to
properties of that (transient) String object, and reading (undefined)
values from it, without error but to no effect.

It might be that the global - var - declaration would avoid the problem
in some browsers, but not according to ECMA 262, where section 10.1.3
reads "If there is already a property of the Variable object with the
name of a declared variable, the value of the property and its
attributes are not changed.", in the context of variable instantiation.
(The global object is used as the "Variable" object in global execution
contexts.)

Richard.
Jul 23 '05 #7
On Wed, 4 Aug 2004 14:11:59 +0100, Richard Cornford
<Ri*****@litote s.demon.co.uk> wrote:
Michael Winter wrote:
<snip>
status[0] = new Pair('outlook', 'start');

<snip>

As an observation in passing, the global variable identifier - status -
corresponds with the - status - property of the window object. As -
window.status - is one of those properties with an overloaded setter
assigning an Array to the property might just have the Array
type-converted to a string and the result displayed in the window's
status bar. And attempts to read the property might be returning a
string primitive, subsequently type-converted to a String object when
used with bracket notation property accessors, allowing assignment to
properties of that (transient) String object, and reading (undefined)
values from it, without error but to no effect.

It might be that the global - var - declaration would avoid the problem
in some browsers, but not according to ECMA 262, where section 10.1.3
reads "If there is already a property of the Variable object with the
name of a declared variable, the value of the property and its
attributes are not changed.", in the context of variable instantiation.
(The global object is used as the "Variable" object in global execution
contexts.)


So in other (shorter) words, use a name such as 'state' to avoid any
conflict. :)

Yet another thing I've overlooked today.

Thanks,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail
Jul 23 '05 #8
Michael Winter wrote:
<snip>
So in other (shorter) words, use a name such as 'state'
to avoid any conflict. :)

<snip>

Yes, or keep the variable out of the global scope.

Richard.
Jul 23 '05 #9
ivanhoe wrote:
Thomas 'PointedEars' Lahn <Po*********@nu rfuerspam.de> wrote [...]:
But the problem with your code and even with my code is that you always
add elements to the array, even if the required data is already added.
This looks like a problem suited more for Object objects instead of Array
objects [...]


maybe it could be solved by using hashes(associat ive arrays), instead of
status[status.length] = [t.name, t.value];
one could do
status[name]=value;
so that status always keeps selected values from each name group, no duplicates..


You are talking about Object/user-defined objects which I already mentioned
(yet using `status' as identifier is error-prone, there is a property of the
`window'/global object of that name in browsers). ECMAScript/J(ava)Script
has no built-in concept of associative arrays (as e.g. in PHP) or hashes (Perl).

And please, <http://netmeister.org/news/learn2quote.htm l>.
PointedEars
Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
8743
by: Michael Albanese | last post by:
I am developing an application to handle my compay's OSHA reporting requirements. Some of the input criteria are technical and narowly defined, so I was trying to prvide what i call "Context Sensitive Help" by providing a short instructional message based on which control has Focus. Because of the way that a Radio Button List control is rendered in the browser i can not attach an onFocus event
8
1979
by: shandain | last post by:
Ok, I have done this a million times, and I can't get this to work... I hope I am just being an idiot and missing something... but I can't see it... so please call me an idiot and tell me what it is... <code> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML> <HEAD> <title>Online Orientation Quiz</title> <meta http-equiv="Content-Type" content="text/html;
4
11027
by: Jared | last post by:
Radio Button or Check Box and Event Procedures I need to insert either radio buttons or check boxes onto my form. I'm not sure which to use, or if there are other options. I am using the buttons to: if one is clicked, its corresponding information will become available on another document, if it's not clicked no information will be provided. If multiple buttons are clicked their information will available on the same document. I'm not...
4
1324
by: Dino Buljubasic | last post by:
Hi, I had to improvise a control that looks like a list view with each row holding 3 radio buttons. To do this I am using a panel (mainPanel) that holds other panels representing rows (rowPanel). So, I fetch database and for each item, I add a rowPenel with 3 radio buttons to the mainPanel. Now, for each row, there must be one radio button selected. To check
18
2254
by: Ed Jay | last post by:
<disclaimer>js newbie</disclaimer> My page has a form comprised of several radio buttons. I want to poll the buttons to determine which button was selected and convert its value to a string. I then want to use the string on the same page. My script is: function checkRadio(field) { for(var i=0; i < field.length; i++) {
1
9613
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej and I was wondering if anyone here would be able to give me some tips for young players such as myself, for learning the language. Is this the best Newsgroup for support with JAVA?
2
1681
by: Tyla | last post by:
I have been teaching myself javascript and i am having a huge problem with this form. All it has to do is check that every field has something in it and then post, but as soon as it gets to the document.getElementById('form1').submit(); it throws an error and say that the object is not supported. Here is the code: <script language="javascript"> function check_form(){ if(document.getElementById('name').value == ""){ alert("Enter...
3
3769
by: Abersparky | last post by:
I'm having a problem with multiple radio button groups within a while loop. I have the page made so it pulls from a database the row values - one of which is a Y or N value. I have a radio button group for the Y and N value for each row of the database that gets pulled but only the last row will populate the value retrieved from the db. I know that I have to create a unique name for the radio buttons for each row - but I don't know how...
0
8049
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7985
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8128
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8322
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5997
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3953
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2461
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
1
1574
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1316
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.