I'm getting an error Uncaught error unexpected token ( in script tag on string with for loop
<script type="text/javascript">
var offer_foto_index = 'slide_offer_preview_1';
var slide_offer_preview_ = [];
var photo_array = <%= @photos.to_json.html_safe %>;
var offer_foto = {
for(var i = 0; i < <%= @photos.length %>; i++) {
slide_offer_preview_[i] = photo_array[i];
}
}
</script>
I need an offer_photo variable which should contain arrays slide_offer_preview_1, slide_offer_preview_2, slide_offer_preview_3 etc.
Where did I make a mistake? Thanks!
2 Answers 2
Answer based on your comment: "...I need a offer_photo variable. It should contain array like slide_offer_preview_1, slide_offer_preview_2 etc."
<script type="text/javascript">
var offer_foto_index = 'slide_offer_preview_1';
var slide_offer_preview_ = [];
var photo_array = <%= @photos.to_json.html_safe %>;
var offer_foto = {};
for(var i = 0; i < <%= @photos.length %>; i++) {
offer_photo['slide_offer_preview_' + i] = photo_array[i];
}
</script>
Or if you need it to be in one array within "offer_foto" object:
var offer_foto = {};
offer_foto.slide_offer_preview_ = [];
for(var i = 0; i < <%= @photos.length %>; i++) {
offer_photo.slide_offer_preview_[0] = photo_array[i];
}
Comments
Without seeing what your actual rendered code looks like, this will be difficult. But my guess is you should be using photo_array instead of @photos:
<script type="text/javascript">
var offer_foto_index = 'slide_offer_preview_1';
var slide_offer_preview_ = [];
var photo_array = <%= @photos.to_json.html_safe %>;
//var offer_foto = { - I've commented this because you don't need it...
for(var i = 0; i < photo_array.length; i++) {
slide_offer_preview_[i] = photo_array[i];
}
//}
</script>
3 Comments
offer_photo variable. It should contain array like slide_offer_preview_1, slide_offer_preview_2 etc.var
offer_fotowith no properties, instead, you put an instruction, that's not permitted