Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing array as an argument to a method

Status
Not open for further replies.

prolog2345

Programmer
Jan 2, 2006
7
YU
Hi there, i am new to javascript and i was heaving a problem with the code below, i get a runtime exception saying 'id is null or not an object' ( i try to acces 'id' from the 'function formErr(arguments)' )
this does not make sence to me since everything seems quite straight forward. Perhaps when i pass the array to the function and than access it i must down cast or something.
Does anyone know the answer to this


thanks.
//---------------------------------------

function checkLoginCredentials()
{

checkName( document.getElementById('username') , document.getElementById('password')
)

}


function checkName(username, password)
{
if ( ( username.value ) && ( password.value ) )
return true;

var array = new Array(2);
if ( username.value )
array[0] = username;
if ( password.value )
array[1] =password;

// return formErr(username , password );
return formErr(array);
}


function formErr( arguments )
{
var message = "The following fields are empty:\n";

var entries = arguments;

for(i=0; i<entries.length; i++)
{
message += entries.id + "\n";
}
alert(message);
return false;
}

 
arguments" is a reserved keyword in javascript, and it is an array of the arguments passed to a function.

use a different variable name if you want to keep your function as-is:

function formErr( myArray )
{
var message = "The following fields are empty:\n";

for(i=0; i < myArray.length; i++)
{
message += myArray.id + "\n";
}
alert(message);
return false;
}



-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
thanks for you answer jeff.

but i still get the same error even though i changed the name from 'arguments' to 'myArray'.
still getting the same runtime eception:'id' is null or not an object'.

its strange but if instead of passing the array to 'function formErr ' i pass the elemts in the array
and than use the arguments array to access the values being passed everything works, the code below shows what i mean( and it works)-- but still this is not enough to solve the problem which i have.


function formErr()
{
var message = "The following fields are empty:\n";

var entries = arguments.;

for(i=0; i<entries.length; i++)
{
message += entries.id + "\n";
}
alert(message);
return false;
}

 
--------frogot to atach the rest of the code

thanks for you answer jeff.

but i still get the same error even though i changed the name from 'arguments' to 'myArray'.
still getting the same runtime eception:'id' is null or not an object'.

its strange but if instead of passing the array to 'function formErr ' i pass the elemts in the array
and than use the arguments array to access the values being passed everything works, the code below shows what i mean( and it works)-- but still this is not enough to solve the problem which i have.


// JavaScript Document


function checkLoginCredentials()
{
if (
checkName(
document.getElementById('username') , document.getElementById('password')
)
)
{
// document.getElementById('myform').submit();
}
}


function checkName(username, password)
{
if ( ( username.value ) && ( password.value ) )
return true;

var array = new Array(2);
if ( username.value )
// add this element to the array
array[0] = username;
if ( password.value )
array[1] =password;

// return formErr(username , password );
return formErr(username , password);
}


function formErr( myArray )
{
var message = "The following fields are empty:\n";

myArray = formErr.arguments;
for(i=0; i < myArray.length; i++)
{
message += myArray.id + "\n";
}
alert(message);
return false;
}

 
my example works in Fx 1.5 and IE6 using this for testing:
Code:
function foo() {
	var ar = new Array();
	ar[0] = document.getElementById("username");
	ar[1] = document.getElementById("password");
	formErr( ar );
}


<input type="text" name="username" id="username" />
<input type="text" name="password" id="password" />

make sure your elements have an "id" exactly as you are referencing them.

alternatively you can use your method of just passing each element as an argument and directly accessing via "arguments", though this can make your code harder to understand later since you don't have explicitly named arguments.




-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top