0

This is a question involving a loop that I've seen asked a lot, i just can't seem to find anything that helps my situation.

I've built a very simple app which calls the Instagram API when the user inputs a hashtag. The below displays the images and assigns the URL to the image.

    for ( i = 0; i < response.data.length; i++) {
        imageDisplay = response.data[i].images.low_resolution.url;
        imageUrl = response.data[i].link;
        $instagram.append( '<img src="' + imageDisplay + '"/>');
        $('img').wrap("<a href="+ imageUrl + "></a>");
    };
})

Here's where the problem is, every image sends the user through to the last url result. I need to return the result with each loop but I can't figure out how.

2 Answers 2

1

I'm not a jQuery whiz, but I think this code:

$('img').wrap("<a href="+ imageUrl + "></a>");

... has the effect of wrapping your link around EVERY image tag. So every time you go through the loop, you're re-wrapping all your images.

You need to identify the particular image tag you just created, and wrap your link around that.

No doubt someone will correct me if I've got this wrong.

Sign up to request clarification or add additional context in comments.

Comments

0

The problem seems to be in $('img').wrap("<a href="+ imageUrl + "></a>"); you select all img tags this way. The correct way would be

$instagram.append( '<a href="+ imageUrl + "><img src="' + imageDisplay + '"/></a>');

Comments

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.