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.