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

T

tabonni

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="javascript">
<!--
var temp = new Array(2)
var status = new Array();

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

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

<tr>
<td>Microsoft Word: </td>
<td>
<input type='radio' name="word" value="START"
onClick="getRadios(document.training)"> START
<input type='radio' name="word" value="COMPLETED"
onClick="getRadios(document.training)"> COMPLETED
<input type='radio' name="word" value="IN PROGRESS"
onClick="getRadios(document.training)"> INPROGRESS
</td>
</tr>

</table>
</form>

</body>
</html>
 
T

Thomas 'PointedEars' Lahn

tabonni said:
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="javascript">


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<paramform.elements.length; iElement++ )
{
if( paramform.elements[iElement].checked )
{
temp[0] = paramform.elements[iElement].name;
temp[1] = paramform.elements[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="getRadios(document.training)"> START
[...]>
</form>

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

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

<form ... onclick="getRadios(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
 
M

Mick White

tabonni said:
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="javascript">
<!--
var temp = new Array(2)
var status = new Array();

function getRadios( paramform )
{
for( var iElement=0; iElement<paramform.elements.length; iElement++ )
{
if( paramform.elements[iElement].checked )
{
temp[0] = paramform.elements[iElement].name;
temp[1] = paramform.elements[iElement].value;
status[status.length] = temp;
}
}
}
Along with Thomas' observations, why not terminate the loop when you
find an element checked?

Mick
 
T

tabonni

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 said:
tabonni said:
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="javascript">
<!--
var temp = new Array(2)
var status = new Array();

function getRadios( paramform )
{
for( var iElement=0; iElement<paramform.elements.length; iElement++ )
{
if( paramform.elements[iElement].checked )
{
temp[0] = paramform.elements[iElement].name;
temp[1] = paramform.elements[iElement].value;
status[status.length] = temp;
}
}
}
Along with Thomas' observations, why not terminate the loop when you
find an element checked?

Mick
 
I

ivanhoe

Thomas 'PointedEars' Lahn said:
tabonni said:
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="javascript">


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<paramform.elements.length; iElement++ )
{
if( paramform.elements[iElement].checked )
{
temp[0] = paramform.elements[iElement].name;
temp[1] = paramform.elements[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="getRadios(document.training)"> START
[...]>
</form>

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

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

<form ... onclick="getRadios(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(associative 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..
 
M

Michael Winter

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(status.name, status.value);
}

Much cleaner and easier to understand.

Mike
 
R

Richard Cornford

Michael Winter wrote:
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.
 
M

Michael Winter

Michael Winter wrote:
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
 
R

Richard Cornford

Michael Winter wrote:
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.
 
T

Thomas 'PointedEars' Lahn

ivanhoe said:
Thomas 'PointedEars' Lahn said:
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(associative 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.html>.


PointedEars
 
T

tabonni

Thank you all of you.

My previous problem, it seems is solved. I figured out another way to
get values from Radio button and set cookie last night. May be it is a
stupid way. But, it works.

Now, I got another problem. I haven't figure out how can I set the
values in the cookies back to radio form using getCookie() method. My
purpose is after the user close the browser and open a new browser
next time. The previous values are set. And also, I try to put an
onload() method in <body> e.g. <body onload="setForm();". I got an
error "theForm.elements... is null or not a object". Could anyone give
me any suggestions and comments? THANKS IN ADVANCE.

My new code is as follow:

<body>
<script language="javascript">
var theForm;

/**
Store checked values index into storeIndex array
**/
function getSelectedRadio()
{
var storeIndex = new Array();
for( var i=0; i<theForm.elements.length; i++ ){
if( (theForm.elements.checked) ||
(theForm.elements.defaultChecked) )
storeIndex[storeIndex.length]= i;
}
return storeIndex;
}

function createCookie()
{
var indexArray = new Array();
indexArray = getSelectedRadio();
for( var s=0; s<indexArray.length; s++ )
setCookie( theForm.elements[indexArray].name,
theForm.elements[indexArray ].value );
}

function bakeCookie(f)
{
theForm=f;
var yes_or_no = window.confirm( "Are you sure to change your
status?" );
if( yes_or_no == true )
{
createCookie();
}
}

</script>
<form>
<table>

<tr>
<td>Microsoft Outlook: </td>
<td>
<input type='radio' name="outlook" value="START"> START
<input type='radio' name="outlook" value="COMPLETED"> COMPLETED
<input type='radio' name="outlook" value="IN PROGRESS"> INPROGRESS
</td>
</tr>

<tr>
<td>Microsoft Word: </td>
<td>
<input type='radio' name="word" value="START"> START
<input type='radio' name="word" value="COMPLETED"> COMPLETED
<input type='radio' name="word" value="IN PROGRESS"> INPROGRESS
</td>
</tr>
</table>
</form>
</body>
 
H

Harag

Michael Winter wrote:
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.


Even though javascript isn't a typed language like vb script/c++ I
find it usefull to put type infront of the variable names

aStatus // an Array
iStatus // an integer
fStatus // a float
bStatus // a boolean
sStatus // a string
oStatus // an object

etc

Some others I put a g_ or a m_ infront as well

eg

g_iStatus
m_iStatus

I use g_ when the variable is declared in a file other than the main
file (eg a constants.js file) and I use m_ for variables declared in
the main file that ain't in functions if a variable is declared in a
function then I don't bother with g_ or m_

g_ = global
m_ = module



Just my little tip.

HTH

Al
 
T

Thomas 'PointedEars' Lahn

Thomas said:
Consider this:

function getRadios(f)
{
// Avoid global and undeclared variables where you can!
var e;
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];
}
}
}
}
 
T

Thomas 'PointedEars' Lahn

tabonni said:
My previous problem, it seems is solved. I figured out another way to
get values from Radio button and set cookie last night. May be it is a
stupid way. But, it works.

From what I read it does not seem to work:
Now, I got another problem. I haven't figure out how can I set the
values in the cookies back to radio form using getCookie() method.

Which is? (not a built-in)
[...]
THANKS IN ADVANCE.

Please don't SHOUT.
My new code is as follow:

<body>
<script language="javascript">

This is still invalid. Have you even considered to use
[...]
[Top post]

Please read the <http://jibbering.com/faq/> and learn how to post.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Harag said:
Even though javascript isn't a typed language like vb script/c++ I
find it usefull to put type infront of the variable names

[...]
iStatus // an integer
fStatus // a float

There are no real integers in ECMAScript implementations, all numbers are
floats (read the FAQ, ask Google). I use the prefix "i" only if I expect
the fractional part to be zero. For the other numbers I use "n" as prefix.
Full ACK to the rest of prefixes, I tend to use the same style.


PointedEars
 
H

Harag

Harag said:
Even though javascript isn't a typed language like vb script/c++ I
find it usefull to put type infront of the variable names

[...]
iStatus // an integer
fStatus // a float

There are no real integers in ECMAScript implementations, all numbers are
floats (read the FAQ, ask Google). I use the prefix "i" only if I expect
the fractional part to be zero. For the other numbers I use "n" as prefix.
Full ACK to the rest of prefixes, I tend to use the same style.


Yep, you entirly right, but coming from a VB background I've kept the
habit of using 'i' & 'f' for the numbers, Since starting JavaScript
I've taken the prefix as to meaning exactly what you said, so where I
use 'i' prefix I expect the factional part to be a zero ( or have
zeroed it manually) eg

iAge = parseInt(Response.Request('Age').Item,10);
if (isNAN(iAge)) iAge = 21;

Al.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top