How to load multiple .html pages with ajax

1

I am using this code to load a .html page, but it does not create several as I would like, I have the feeling that it only manages to create it once. I would like to create several of them.

for(var i = 0; i < 4; i++)
    {
        $.post('linha.html', function (html) 
        {
            $('#baseTorrent').html(html);
        });
    }
    
asked by anonymous 14.01.2017 / 21:17

1 answer

2

This line replaces all contents of #baseTorrent with what has been passed:

$('#baseTorrent').html(html);

From what you say, you want to add more content, not substitute. To do this, use append() :

$('#baseTorrent').append(html);

With this change, you will have this HTML repeated 4 times within #baseTorrent .

Another thing, if it is to repeat the HTML, you do not have to go get it four times on the server. Better to do so:

$.post('linha.html', function (html) 
{
    for(var i=0; i<4; i++) {
        $('#baseTorrent').append(html);
    }
});
    
14.01.2017 / 22:06