how to add a number onto a variable name and cycle upward: x1 x2 x3 etc in jQuery
I need to change a variable name each time a piece of code loops, and increment it by one.
I'm not sure how to append it onto the variable name, so in my example each time it loops ( once for each div that's a direct child of body ) it assigns a new variable name --> myFunction1 myFunction2 myFunction3 and so-on.
Here's what i have:
$(document).ready(function() {
$('body > div').each(function(i) {
开发者_如何学Go alert(i);
// for each carousel
function myFunction+i(carousel) {
// whatever
})
};
});
});
Not exactly sure why you would do that, but it can be done like this:
window["myFunction"+i] = function(){...};
assuming you want these functions to be global. Then it can be called as normal:
myFunction1();
精彩评论