Reputation: 633
There are topics about this, but not realy pointed at my problem.
I'm using iScroll, a jquery plugin. For every scrollbox on my page I need to assign a unique variable. In this case it's gonna be myScroll1, myScroll2 etc.
To make it easily editable, I want the variables to be automatically generated giving the number of div's in wich the scrollbars are in. So, let's say I have 2 divs with these scrollbars. I use the .size() method to count the divs. Then I want a while loop to make the variables. I now have something like this:
var i = 0;
var item_number = $(".portfolio_item").size();
var myScroll = [];
while( i < item_number ) {
myScroll[i];
i++;
}
Sooo, I don't want anything generated in the HTML, just the variables. They have to become myScroll1, myScroll2, myScroll3. Depending on the amount of .portfolio_item divs.
I have tried this with php, that's a bit easier for me. But!! I can't use javascript variables in php. So I can't count the amount of .portfolio_item divs :( Thank you! :)
Upvotes: 1
Views: 832
Reputation: 207521
Bad practice, but you can do this
var i = 0;
var item_number = $(".portfolio_item").size();
while( i < item_number ) {
window["myScroll" + i] = "foo" + i;
i++;
}
alert(myScroll0);
better practice is to use a namespace and add to it.
var i = 0;
var item_number = $(".portfolio_item").size();
var foo = {};
while( i < item_number ) {
foo["myScroll" + i] = "foo" + i;
i++;
}
alert(foo.myScroll0);
Upvotes: 2