0

I'm working on a personal project, and had a question regarding jQuery and using it to loop through a JSON array.

I have the following JSON array:

var thumbDir = [
'"https://www.location.com/1.jpg"',
'"https://www.location.com/2.jpg"', 
'"https://www.location.com/3.jpg"'
];

This array will have the locations for the thumbnail images for the homepage. I want to loop through them with jQuery and appendTo the following:

<div id="thumbContainer"></div>

I thought I had it figured out, with the following code, but it's not working, and I'm not sure where I'm going wrong.

            $(document).ready(function(){
                $.each(thumbDir, function(index, value){
                    $("#thumbContainer").appendTo('<img src=' + value + ' height="120" width="172">');
            });

I'm pretty new to this, and it's my first project, so I'm just learning as I go. Any help would be appreciated!

2
  • go to jQuery API and look up what appendTo does. You want append Commented Sep 23, 2014 at 23:32
  • @EvoD not the issue. $('element').appendTo('target') will append 'element' to 'target'. Since you want to add imgs to the div, you need to use $('target').append('element') Commented Sep 23, 2014 at 23:33

2 Answers 2

1

You're using .appendTo when you should be using .append.

$("#thumbContainer").append('img src=' + value + ' height="120" width="172">');

Also, I'm assuming you simply omitted the ending brace/parenthesis.

Working Fiddle: http://jsfiddle.net/c5kozmaq/

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

Comments

1

use append instead of appendTo.

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.