No, no. This does not work. Please get familiar with the jQuery Library.
$('foo');
The $()
function excepts a CSS Selector as a Parameter and returns an jQuery object, you can't put just variables into it. Your Javascripts and your HTML are two different things.
If you want to iterate over arrays in your code use the following:
var a = ["Hello","World"];
for (var i = 0; i < a.length; i++) {
alert(a[i]);
}
There are plenty of ways to iterate over an array, see this for a reference.
Second: As mentioned using alert()
to debug is not a good idea, especially when looping something.
Use console.log('string', variable)
instead when you are using Chrome or Firefox. It's writing your variables straight into the console and it also logs complete objects which is not possible with alert()
.
Update after comment: It's not possible to iterate over all existing arrays in your javascript. That's because you can't list all existing variables. One problem is, that your creating a new scope with every new function.
The variables inside the function don't know about the ones outside. So for example this is a valid piece of code:
var a = 5;
function foo() {
var a = 6;
return a;
}
console.log(a); // prints "5"
console.log(foo()); // prints "6"
Both variables are called a
, but they contain different data.
For further info see this answer: https://stackoverflow.com/a/2051693/735226
And just by the way: Why do you even want to know how many arrays there are in your scripts? And since objects can contain arrays like this
var objWithArray = {
foo:5,
bar:[3, 4, 5, 6, 7] // objWithArray.bar would be an array
};
there is no way to get all arrays. Javascript is a typeless and mutable language.