I'd like to get the value of an array depending on the index of the ul element. I'll try to explain myself:
var descgal0 = ["3456", "463", "6557", "5242"];
var descgal1 = ["gfdgfd" "gfgdfg", "gfdg", "gfdg"];
$("#gal-content-all section ul li img").click(function() {
var index = $(this).parent().index();
var indexul = $(this).parent().parent().parent().index();
});
The problem is
$("#gal-zoom h4").text(descgal0[index]); //WORKS!
$("#gal-zoom h4").text("descgal"+indexul+[index]); //DOESN'T WORK :(
How could I get the array[li] value depending the ul I'm clicking on...?? Thank you!!!!
Radhika
2,5893 gold badges26 silver badges30 bronze badges
-
1Thanks @Rads for the grammatic correction. English is not my native language ;)Gaby– Gaby2014年02月19日 13:22:17 +00:00Commented Feb 19, 2014 at 13:22
4 Answers 4
You can use array of arrays since you have sequential indexes
var descgals = [
["3456", "463", "6557", "5242"],
["gfdgfd", "gfgdfg", "gfdg", "gfdg"]
];
$("#gal-content-all section ul li img").click(function () {
var index = $(this).parent().index();
var indexul = $(this).parent().parent().parent().index();
$("#gal-zoom h4").text(descgals[indexul][index]);
});
answered Feb 18, 2014 at 12:27
Arun P Johny
389k68 gold badges532 silver badges532 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Considering your data model eval seems like the only way :
$("#gal-zoom h4").text(descgal0[index]); //WORKS!
$("#gal-zoom h4").text(eval("descgal"+indexul+"["+ index + "]")); //SHOULD WORK :D
But, if you have control over the data model, you could go with something prettier than an eval, like this :
var descgal = [["3456", "463", "6557", "5242"],["gfdgfd" "gfgdfg", "gfdg", "gfdg"]];
$("#gal-zoom h4").text(descgal[indexul][index]);
answered Feb 18, 2014 at 12:30
Florian F.
4,70029 silver badges50 bronze badges
Comments
you have to use eval, try this:
$("#gal-zoom h4").text(eval("descgal" + indexul + "[" + index + "]"));
instead of this:
$("#gal-zoom h4").text("descgal"+indexul+[index]);
answered Feb 18, 2014 at 12:30
Amir Sherafatian
2,0932 gold badges21 silver badges32 bronze badges
Comments
Try this way
$("#gal-zoom h4").text(descgal0[index]);
arrName="descgal"+indexul;
$("#gal-zoom h4").text(arrName[index]);
Happy Coding:)
answered Feb 18, 2014 at 12:31
dreamweiver
5,9922 gold badges27 silver badges39 bronze badges
1 Comment
Gaby
Thanks for your reply, the solution was the Arun P Johny's option
lang-js