PDA

View Full Version : how to pass x amount of varialbes thorugh a function


emehrkay
08-31-2005, 04:10 PM
I want to define the number of variables that goes into a function. I have this scrip that creats html tabs and you have to define how many tabs will be used:

var panels = new Array('panel1', 'panel2');

i put the script in its own page - tabs.js - so that i can call it multiple times etc. what i want to do is define how many panels will be used so i tried this:

function num_panels(number){
var panelsGlobal = new Array(number);
}

and in the body onLoad tag i put this

num_panels('\'panel1\',\'panel2\'');

it doesnt work. what did i do wrong? is var panelsGlobal correct so that panels can be passed through to differnt functions?

nikkiH
08-31-2005, 04:14 PM
Well, you're passing two strings into a function that expects an integer... ;)

Send it 2, not the two strings.

emehrkay
08-31-2005, 04:19 PM
in my new function im trying to recreate

var panels = new Array('panel1', 'panel2');

i put the outside ' then slashed out the interal \' so that number would = 'panel1','panel2'

i dont quite understand what you mean

nikkiH
08-31-2005, 04:20 PM
Wait, that doesn't fix your main issue.

Do this.

var panelsGlobal; // global if outside any functions or anything

function setPanels(arr){
panelsGlobal = arr;
}

function getPanels() {
return panelsGlobal; // just in case you want to get it from another window or something
}

Now you can do this inside functions or whatever.
var panels = new Array('panel1', 'panel2');
setPanels(panels);

emehrkay
08-31-2005, 04:30 PM
thanks but i dont think thats going to help me.

what i want to do is to be able to say page a will have 2 panels ('panel1','panel2') page b will have 10 panels ('panel1','panel2','panel3'....) and have this line come out to

var panels = new Array('panel1', 'panel2');

or pageb

var panels = new Array('panel1', 'panel2',...*to 10*);

nikkiH
08-31-2005, 04:51 PM
Ah, I see.



function loadIt(num)
{
var tmpArray = new Array(num);
for (var i=1; i<=num; i++)
{
tmpArray[i-1] = 'panel'+i;
}
setPanels(tmpArray); // see above post
}

<body onLoad="loadIt(10)">


You don't HAVE to use that shortcut syntax to make an array. What matters is that you HAVE the array for the stuff that needs it, not how you construct it.


Jeez, I can't spell or type for squat today...

emehrkay
08-31-2005, 05:18 PM
Ah, I see.



function loadIt(num)
{
var tmpArray = new Array(num);
for (var i=1; i<=num; i++)
{
tmpArray[i-1] = 'panel'+i;
}
setPanels(tmpArray); // see above post
}

<body onLoad="loadIt(10)">


You don't HAVE to use that shortcut syntax to make an array. What matters is that you HAVE the array for the stuff that needs it, not how you construct it.


Jeez, I can't spell or type for squat today...

thanks, the logic makes sense but still doesnt work. what does var tmpArray = new Array(num);that do? and does this tmpArray[i-1] = 'panel'+i; create an array?