I’m trying to run series of functions i placed on a variable.
Here’s a (demo)
Code:
function pass_me(x) {
alert(x);
};
var colors = new Array();
var colors = ["pass_me('yellow');", "pass_me('green');", "pass_me('blue');"]
for(var i = 0; i <= colors.length; i++) {
window[colors[i]]();
}
unfortunately, I can’t make it run. Any idea what’s wrong in my code?
Thanks
The story behind the array variable of function, is because i copied it from a dynamic element. This way it will only run what function is already in the page. So if pass_me(“red”) is not present, it will not be run.
Example:
<img src="img/something.jpg" ondblclick="pass_me("yellow")" />
<img src="img/something.jpg" ondblclick="pass_me("green")" />
<img src="img/something.jpg" ondblclick="pass_me("blue")" />
then I just use:
jQuery("td img").each(function(){
colors.push(jQuery(this).attr("ondblclick"));
})
Either change the array members to a handler like this:
This way you can have multiple handlers in the same array with different parameters per each handler.
Or initialize the members before calling them because data type of the members is string, not Function. like this:
Hope it helps you.
Cheers