469,903 Members | 1,791 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Pass radiobutton control array to function?

Newbie question here...

I have a form with some radio buttons. To verify that at least one of the
buttons was chosen I use the following code ("f" is my form object) :

var btnChosen;
for (count = 0; count <= 1; count++) {
if (eval(f.RadioButtons[count].checked)) {
btnChosen = true;
}
}
if (!btnChosen) { /* Code here to warn no button selected */}

....I'd like to move this to a function. Can I pass the "f.RadionButtons"
array to a function in Javascript? This way I could simply say:

if (! fncSelected(f.RadioButtons)) { /* Code here to warn no button selected
*/}
Jul 20 '05 #1
5 2886
On Mon, 01 Mar 2004 23:49:34 GMT, Seeker <do*******@me.here> wrote:
I have a form with some radio buttons. To verify that at least one of the
buttons was chosen I use the following code ("f" is my form object) :

var btnChosen;
for (count = 0; count <= 1; count++) {
This declares 'count' to be a global variable. I very much doubt you want
that. Use 'var': for (var count = 0;....

I also doubt that you only want to check the first radio button.

...; count < f.RadioButtons.length; ...

would be more appropriate.
if (eval(f.RadioButtons[count].checked)) {
Why are you using eval() here?

if( f.RadioButtons[ count ].checked ) {

is sufficient. There will very rarely be any need for you to use eval().
If you think you need it, you probably don't understand the language well
enough. That's not meant to be derogatory: the better alternatives can be
a little obscure, but they will be better nevertheless.

There are previous threads on the topic of proper eval() usage. Use the
Google Groups archive if you're interested.
btnChosen = true;
}
}
if (!btnChosen) { /* Code here to warn no button selected */}

...I'd like to move this to a function. Can I pass the "f.RadionButtons"
array to a function in Javascript? [...]


'f.RadioButtons' will actually be a collection[1], but in short: yes, you
can. Below is a more efficient version of your original snippet.

function isChecked( group ) {
var size = group.length;

for( var i = 0; i < size; ++i ) {
if( group[ i ].checked ) return true;
}
return false;
}

function validate( form ) {
alert( 'Button checked: ' + isChecked( form.RadioButtons ));
}
...
<button type="button" onclick="validate(this.form);">

Hope that helps,
Mike
[1] Collections can be thought of as simplified versions of arrays. You
can index the elements, and determine the size (length) but, unlike
arrays, you can't use methods such as join() and shift().

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #2
Seeker wrote:
I have a form with some radio buttons. To verify that at least one of the
buttons was chosen I use the following code ("f" is my form object) :

var btnChosen;
for (count = 0; count <= 1; count++) {
if (eval(f.RadioButtons[count].checked)) {
btnChosen = true;
}
}
if (!btnChosen) { /* Code here to warn no button selected */}

...I'd like to move this to a function. Can I pass the "f.RadionButtons"
array to a function in Javascript? This way I could simply say:


http://academ.hvcc.edu/~kantopet/jav...rent=js+arrays

Pass by Value / Pass by Reference

When passing a single value as an argument to a functions you must refer
to the element being passed, not the array itself. If you just use the
array name, then what you are passing is the array, not its values.

It is also important to understand the difference between pass by value
and pass by reference with arrays.

Primitive data types are passed by value. This means that when you pass
them to a function or use them in an assignment statement, what gets
passed is the value, not the variable itself. A copy of the value is
made to be passed to the function or variable.

Arrays and objects, on the other hand, are passed by reference. This
means that when you pass them to a function or use them in an assignment
statement, the reference to the variable is passed. No new copy is
created. Instead, the function argument or variable is pointed to, or
set to reference, the same location in memory. Thus, any changes you
make affect the original.

Array elements are passed based on the value of its contents, it is the
array itself that is automatically passed by reference.

.... the web page has a few simple examples of passing arrays to functions.

Jul 20 '05 #3

"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message
news:op**************@news-text.blueyonder.co.uk...
On Mon, 01 Mar 2004 23:49:34 GMT, Seeker <do*******@me.here> wrote:
I have a form with some radio buttons. To verify that at least one of the buttons was chosen I use the following code ("f" is my form object) :

var btnChosen;
for (count = 0; count <= 1; count++) {


This declares 'count' to be a global variable. I very much doubt you want
that. Use 'var': for (var count = 0;....

I also doubt that you only want to check the first radio button.


Thanks... It's not my code. There are a great many things I don't like about
the script I'm modifying. Hopefully I can clean it up.

...I'd like to move this to a function. Can I pass the "f.RadionButtons"
array to a function in Javascript? [...]


'f.RadioButtons' will actually be a collection[1], but in short: yes, you
can. Below is a more efficient version of your original snippet.


I appreciate the help. Trying to prove myself useful to get out of a deadend
position here.

Thanks!
Jul 20 '05 #4

"mscir" <ms***@access4less.net> wrote in message
news:10*************@corp.supernews.com...
Seeker wrote:
I have a form with some radio buttons. To verify that at least one of the buttons was chosen I use the following code ("f" is my form object) :

var btnChosen;
for (count = 0; count <= 1; count++) {
if (eval(f.RadioButtons[count].checked)) {
btnChosen = true;
}
}
if (!btnChosen) { /* Code here to warn no button selected */}

...I'd like to move this to a function. Can I pass the "f.RadionButtons"
array to a function in Javascript? This way I could simply say:

http://academ.hvcc.edu/~kantopet/jav...rent=js+arrays <snip>
Array elements are passed based on the value of its contents, it is the array itself that is automatically passed by reference.

... the web page has a few simple examples of passing arrays to functions.


Thanks! It's appreciated!
Jul 20 '05 #5
Lee
Seeker said:

Newbie question here...

I have a form with some radio buttons. To verify that at least one of the
buttons was chosen I use the following code ("f" is my form object) :

var btnChosen;
for (count = 0; count <= 1; count++) {
if (eval(f.RadioButtons[count].checked)) {
btnChosen = true;
}
}
if (!btnChosen) { /* Code here to warn no button selected */}

...I'd like to move this to a function. Can I pass the "f.RadionButtons"
array to a function in Javascript? This way I could simply say:

if (! fncSelected(f.RadioButtons)) { /* Code here to warn no button selected
*/}


Yes, you can do that.
Get rid of the eval(), though. It's not doing anything.

function fncSelected(buttons){
for (i=0;i<buttons.length;i++){
if(buttons[i].checked){
return true;
}
}
return false;
}

However, the best way to make sure that at least one radio button
is selected is to preselect a reasonable default in the HTML.

Jul 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by dfg | last post: by
4 posts views Thread by The Mess | last post: by
4 posts views Thread by Sunny | last post: by
7 posts views Thread by Igor | last post: by
29 posts views Thread by Why Tea | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.