Josh Langley
Josh Langley

Reputation: 27

Looping Through a JSON array with jQuery to appendTo an HTML div?

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!

Upvotes: 0

Views: 418

Answers (2)

use append instead of appendTo.

Upvotes: 1

Stryner
Stryner

Reputation: 7318

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/

Upvotes: 1

Related Questions