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! While you're at it, check out some resources Treehouse students have shared here.

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

JavaScript

Create new element by jQuery each time ajax is triggered

I am struggling with the idea of, adding elements to DOM for each time I triggered an AJAX call by jQuery.

I just want to ask if this is a possible solution or I should not do it through AJAX if I want elements each time I triggered an event.

The code below refresh the text everytime I triggered the AJAX call, I want it to add whole new element instead of refreshing.

The problem is I can't stack the new element each time I called an AJAX function. For example, when I trigger 100 times ajax, I expect 100 new elements will appear, and the real situation is it only refresh each time I trigger the ajax function.

Here is the code I have written.

function successAJAX(response){
        response = $.parseJSON(response);
        var priceHTML = document.createElement('p');
        priceHTML.innerText= "foo";
        $("#priceContainer").append(priceHTML);
    }
function successAJAX(response){
        response = $.parseJSON(response);
        var priceHTML = document.createElement('p');
        priceHTML.innerText= "foo";
        $("#priceContainer").append(priceHTML);
    }