HighDots Forums  

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

Javascript JavaScript language (comp.lang.javascript)


Discuss Re: Please tell me the generic way to check different radio button groups? in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Michael Winter
 
Posts: n/a

Default Re: Please tell me the generic way to check different radio button groups? - 08-04-2004 , 06:47 AM






On 3 Aug 2004 17:14:19 -0700, tabonni <tabonni (AT) yahoo (DOT) com> wrote:

Quote:
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[i].name, status[i].value);
}

Much cleaner and easier to understand.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail


Reply With Quote
  #2  
Old   
Richard Cornford
 
Posts: n/a

Default Re: Please tell me the generic way to check different radio button groups? - 08-04-2004 , 09:11 AM






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




Reply With Quote
  #3  
Old   
Michael Winter
 
Posts: n/a

Default Re: Please tell me the generic way to check different radio button groups? - 08-04-2004 , 11:11 AM



On Wed, 4 Aug 2004 14:11:59 +0100, Richard Cornford
<Richard (AT) litotes (DOT) demon.co.uk> wrote:

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


Reply With Quote
  #4  
Old   
Richard Cornford
 
Posts: n/a

Default Re: Please tell me the generic way to check different radio button groups? - 08-04-2004 , 01:06 PM



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




Reply With Quote
  #5  
Old   
tabonni
 
Posts: n/a

Default Re: Please tell me the generic way to check different radio button groups? - 08-05-2004 , 09:39 AM



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[i].checked) ||
(theForm.elements[i].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[s]].name,
theForm.elements[indexArray [s]].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>




"Richard Cornford" <Richard (AT) litotes (DOT) demon.co.uk> wrote

Quote:
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.

Reply With Quote
  #6  
Old   
Harag
 
Posts: n/a

Default Re: Please tell me the generic way to check different radio button groups? - 08-05-2004 , 01:38 PM



On Wed, 04 Aug 2004 15:11:10 GMT, "Michael Winter"
<M.Winter (AT) blueyonder (DOT) co.invalid> wrote:

Quote:
On Wed, 4 Aug 2004 14:11:59 +0100, Richard Cornford
Richard (AT) litotes (DOT) 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.

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


Reply With Quote
  #7  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: Please tell me the generic way to check different radio button groups? - 08-07-2004 , 07:11 AM



tabonni wrote:

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

Quote:
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)

Quote:
[...]
THANKS IN ADVANCE.
Please don't SHOUT.

Quote:
My new code is as follow:

body
script language="javascript"
This is still invalid. Have you even considered to use
<http://validator.w3.org/>? You cannot expect the DOM
to work with invalid markup.

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


PointedEars


Reply With Quote
  #8  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: Please tell me the generic way to check different radio button groups? - 08-07-2004 , 07:15 AM



Harag wrote:

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


Reply With Quote
  #9  
Old   
Harag
 
Posts: n/a

Default Re: Please tell me the generic way to check different radio button groups? - 08-08-2004 , 05:53 AM



On Sat, 07 Aug 2004 13:15:05 +0200, Thomas 'PointedEars' Lahn
<PointedEars (AT) web (DOT) de> wrote:

Quote:
Harag wrote:

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.


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.