Creating static variables is not an option in this case becouse it would take to long time and ineffective to what im trying to accomplish. I have an Array with images and I'm trying to create a way to make divs according to the length of the array. 1,2,3,4 etc.
var images = ['image1.JPG', 'image2.JPG', 'image3.JPG'];
var totalimages = images.length;
Which will result in an array length of 3. I would like to create 3 variables for this.
for (var i = 0; i > totalimages; i++){
var div[i] = document.createElement('div');
}
This seems not to be working for some reason. I tried to create a div array/list outside the for loop aswell.
var div = [];
for (var i = 0; i > totalimages; i++){
var div[i] = document.createElement('div');
}
Still not working. I dunno why this is not working. Javascript only
EDIT: (not working) i mean it gives me syntax error.
3 Answers 3
You have defined div already. In loop you shouldn't be saying like var div again.
BTW var div[ will cause a syntax error.
Use this
div[i] = document.createElement('div');
instead of
var div[i] = document.createElement('div');
Any way I'll prefer saying this at that place
div.push(document.createElement('div'));
And this will cause i > totalimages an infinitive loop, say i < totalimages instead.
1 Comment
var div[ is simply invalid syntax.i < totalimages
not
i > totalimages
Make sure you don't use var inside the array if you're assigning new values:
var div = [];
for (var i = 0; i < totalimages; i++){
div[i] = document.createElement('div');
}
1 Comment
In short:
var images = ['image1.JPG', 'image2.JPG', 'image3.JPG'];
var totalimages = images.length;
var div = [];
for (var i = 0; i < totalimages; i++){
div.push(document.createElement('div'));
}
varinside the loop.div?