0

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!!!!

1
  • 1
    Thanks @Rads for the grammatic correction. English is not my native language ;) Commented Feb 19, 2014 at 13:22

4 Answers 4

1

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]);
});
Sign up to request clarification or add additional context in comments.

Comments

0

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]);

Comments

0

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]);

Comments

0

Try this way

$("#gal-zoom h4").text(descgal0[index]); 
 arrName="descgal"+indexul;
$("#gal-zoom h4").text(arrName[index]); 

Happy Coding:)

1 Comment

Thanks for your reply, the solution was the Arun P Johny's option

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.