Pass radiobutton control array to function?

S

Seeker

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
*/}
 
L

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.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.
 
M

Michael Winter

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().
 
S

Seeker

Michael Winter said:
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!
 
S

Seeker

mscir said:
Seeker said:
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/javascript/index.php?page=using+js+arrays&parent=js+arrays
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!
 

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
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top