Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Jonathan Huppi
.a{fill-rule:evenodd;}techdegree seal-36
Jonathan Huppi
Front End Web Development Techdegree Graduate 42,048 Points

.append() is replacing elements instead of adding new elements

I am using .each() to loop through objects in an array and then using .append() to add the value of the objects to the DOM. When I load the page the values of the last object are added to the page, but none of the other object values. I used Google's debugger and added several breakpoints through the .each() function and the first object values are being appended but later replaced. Any insight to why .append() isn't behaving as predicted would be appreciated.

var newMembers = [
            "name": "Victoria Chambers",
            "email": "victoria.chambers80@example.com",
            "joinDate": "2016-01-27",
            "img": "img/vchamber.jpg"
            "name": "Dale Byrd",
            "email": "dale.byrd52@example.com",
            "joinDate": "2016-01-23",
            "img": "img/dbyrd.jpg"
            "name": "Dawn Wood",
            "email": "dawn.wood16@example.com",
            "joinDate": "2016-01-20",
            "img": "img/dwood.jpg"
            "name": "Dan Oliver",
            "email": "dan.oliver82@example.com",
            "joinDate": "2016-01-14",
            "img": "img/doliver.jpg"

var $memberDiv = $('<div class="memberDiv"></div>');
var $memberName = $('<div class="memberName"></div>');
var $memberEMail = $('<div class="memberEMail"></div>');
var $memberJoinDate = $('<div class="memberJoinDate"></div>');
var $memberImg = $('<img class="memberImg">');

$.each(newMembers, function() {
    $currentObject = $(this);
    $.each($currentObject, function(){



        $memberImg.attr("src", this.img);

1 Answer

David Bath
David Bath
25,940 Points

If I understand what you are doing, it is because each loop is acting on the same HTML elements you create above in the code. Your creation of new elements needs to occur within the loop, so that there are new HTML elements created for each newMember. You don't really need an inner loop. For each newMember, create elements, do the appending and manipulation of attributes, then attach it to the DOM.