1

Line 35, just before the alert, returns -1. I also tried $(this).index() with the same result. Here is what it should do: Clicking EN.gif should return 4, then grand_array_pics[4] should give me en_array_pics and load the .gifs in that array.

$(document).ready(function () {

    var main_pics = ["AN.gif", "BN.gif", "CN.gif", "DN.gif", "EN.gif", "GN.gif"];

    var starting_pics = ["AN.gif", "CN.gif", "EN.gif"];

    var an_array_pics = ["BN.gif", "EN.gif", "GN.gif", "AN.gif","DN.gif"];
    var bn_array_pics = ["CN.gif", "DN.gif", "GN.gif"];
    var cn_array_pics = ["DN.gif", "GN.gif", "AN.gif", "CN.gif"];
    var dn_array_pics = ["EN.gif", "AN.gif", "CN.gif"];
    var en_array_pics = ["GN.gif", "AN.gif", "CN.gif", "EN.gif"];
    var gn_array_pics = ["AN.gif", "CN.gif", "EN.gif", "GN.gif"];

    var grand_array_pics = [
        an_array_pics,
        bn_array_pics,
        cn_array_pics,
        dn_array_pics,
        en_array_pics,
        gn_array_pics
    ];

    var i = 0;

    for (i = 0; i < starting_pics.length; i++) {
        $("<img/>").attr("src", "images/" + starting_pics[i]).load(function () {
            $(this).appendTo("#main");
            $(this).addClass("pics");
        });
    }

    $("#main").on("click", ".pics", function () {

        var j = $.inArray(this, main_pics);
        alert(j);

        $("#sidebar .pics").remove();
        $(this).clone().appendTo("#train");
        $(this).clone().appendTo("#sidebar");
        $("#main .pics").remove();

        var chosen_pics_array = grand_array_pics[j];
        var count = chosen_pics_array.length;

        var k = 0;

        for (k = 0; k < count; k++) {
            $("<img/>").attr("src", "images/" + chosen_pics_array[k]).load(function () {
                $(this).appendTo("#main");
                $(this).addClass("pics");
            });
        }
    });
});       //end ready
0

2 Answers 2

1

this is the DOM <img> element, while main_pics is an array of strings. It will never be found inside there. Use

var j = $.inArray(this.src.split("/").pop(), main_pics);
Sign up to request clarification or add additional context in comments.

Comments

1

Give this a try. You need to get the name of the file and you're passing the element itself into $.inArray

var j = $.inArray(this.src.substring(this.src.lastIndexOf('/')+1), main_pics);

4 Comments

jsperf for performance difference between substring/lastIndexOf and split/pop
You can't mark both. You can upvote both, but not mark them both as correct.
The arrows above/below the numbers. Above upvotes – meaning it's a useful answer, below downvotes – meaning this doesn't help at all.

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.