How can I pass a array name into a function and read values dynamically?
Dynamically i have generated array as follows
fruits[0] = "Banana";
fruits[1] = "Orange";
fruits[2] = "Apple";
//Hyperlink to activate function below
I have a function in hyeperlink <a href="javascript: activate('1', 'fruits')">Click Me</a>
Below is the function
function activate(idNumber, arrayName)
{
alert('id is:'+ idNumber); **//Working**
alert(arrayName[idNumber]); **//Not working must print fruits[1]**
}
It depends on the scope in which ‘fruits’ is defined.
Assuming it is global,
window[arrayName]will get it whenarrayName=='fruits'. This is becausewindowholds a reference to all global objects.