473,320 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 3100
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Miguel | last post by:
Hello. I have a Javascript function that validates an specific form. As parameters this function receives an array of elements to be checked. Depending on the form in cause, the array could have...
9
by: dfg | last post by:
Hello. I have a control Array of option buttons. I'd like to use them in a Case statement, but I can't get it to work. I can only seem to get it working if I use an if-then-else structure, like...
2
by: sfgsdfg | last post by:
Hello, is there a way to pass a control array to a function? i tried the following which works with normal arrays but not control arrays:( Private Sub Click1_Click() Calc txtControlArray,...
4
by: The Mess | last post by:
I would like to pass a Control array of OptionButtons that I created at run time to a Sub. Say I have Opt(0), Opt(1)......Opt(5) as OptionButtons, is there a way to pass Opt to a function and...
4
by: Sunny | last post by:
Hi, I am at present working on a jsp page which has a single text field. Upon entering a value in the field, i am trigerring an onChangeEvent() which calls a method of javascript. I am also...
7
by: Igor | last post by:
Can I create control array (like TextBox array) in design mode. In vb6 I drow some control at the form and then I copy/paste controls. After that every control have the same name, but they have...
8
by: sandeeps123 | last post by:
I have an array s(0) to s(63). I want to perform an operation: text1(s(i)).backcolor=vbblue. Now Textbox1 is a control array: text1(0) to text1(63). I need to pass S(0) to s(63)...all in a ...
4
by: IRC | last post by:
hey, i am pretty new on javascript as well as PHP, Hey, anyone can you help me, how to pass the javascript array value to php page......... i want to retrieve the values which are arrayed on...
29
by: Why Tea | last post by:
Suppose you have a 2-dimensional array (matrix) in main() and you want to pass it to a function to do some processing, you usually pass it as a pointer to the first element. But, from the function,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.