passing parameter of a function to array

C

cuco

I want to use the parameter of a function as a variable for an array.

Example:
<html>
<head>
my function (parameter){
x=new Array(parameter);
// rest of the code using x
}
</head>
<body>
<script language="javascript">my function(" ' item1' , ' item2' , '
item3' ")</script>
</body>
</html>

It's not working.
 
T

Thomas 'PointedEars' Lahn

cuco said:
I want to use the parameter of a function as a variable for an array.

Example:
<html>
<head>
my function (parameter){
x=new Array(parameter);
// rest of the code using x
}
</head>
<body>
<script language="javascript">my function(" ' item1' , ' item2' , '
item3' ")</script>
</body>
</html>

Your code is strewn with syntax errors.
It's not working.

<http://jibbering.com/faq/notes/posting/#ps1DontWork>
<http://validator.w3.org/>
<http://getfirebug.com/>


PointedEars
 
E

Erwin Moller

I want to use the parameter of a function as a variable for an array.

Example:
<html>
<head>
my function (parameter){
x=new Array(parameter);
// rest of the code using x
}
</head>
<body>
<script language="javascript">my function(" ' item1' , ' item2' , '
item3' ")</script>
</body>
</html>

It's not working.

Oh my, you make a load of mistakes.

1) "my function" is not a legal function name.
2) You forgot to place javascript tags around the part in the header.
3) Stop using language="javascript". Use: type="text/javascript" instead.
4) Use a ; to end a statement.
5) Do not try to pass around the array definition. Pass around a
reference to the array.
6) Get in the habbit of using var. You are poluting the global namespace
without it.

A lot more can be said, but I stop here.


So, for starters, try this:

<html>
<head>
<script type="text/javascript">
myFunction (anArray){
var x=anArray;
// The above is probably nonsense because you could use
// anArray too, instead of x. :)
// rest of the code using x
}
</script>

</head>
<body>

<script type="text/javascript">
var myArray = ['item1' , 'item2' , 'item3'];
myFunction(myArray);
</script>
</body>
</html>


Not tested.
Erwin Moller
 
V

VK

I want to use the parameter of a function as a variable for an array.

As Pointed out, my function() {... is either an error or a typo for
JavaScript

For the rest your actual aim is not clear. If you need a function that
takes any amount of arguments and the exact amount may change at
runtime then use automatically created for you "arguments" list:

<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type"
content="text/html; charset=utf-8">
<script>
function myFunction() {
for (var i=0; i<arguments.length; i++) {
window.alert(arguments);
}
}
</script>
</head>
<body>
<p>
<button type="button" onclick="
myFunction('a', 'b');
">Call with 2 args</button>
</p>
<p>
<button type="button" onclick="
myFunction('a', 'b', 'c');
">Call with 3 args</button>
</p>
</body>
</html>

If you need a function that - as in your example - gets a single
string containing comma-delimited values, parses that string and
stores each found value in an array then it is a whole different
question.
 

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

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top