How to load multiple pages . html with ajax

Asked

Viewed 184 times

1

I’m using this code to upload a page. html, however it does not create several as I would like, I have the feeling that it can only 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);
        });
    }
  • I don’t understand what you want to do. This code searches the same html 4 times and puts it in the same place! The loop makes no sense...

  • 1

    @bfavaretto But it creates 4 distinct htmls or it erases the last created and creates a new ??

  • Delete and recreate. You replace 4x #baseTorrent content

  • Maybe you want $('#baseTorrent'). append(html)

  • Perfect, could you put this as an answer please ??

1 answer

2


This line supersedes the entire content of #baseTorrent for what has been:

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

From what you say, you want to add more content, not replace. For this, use append():

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

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

Another thing, if you’re really going to repeat HTML, you don’t have to get it four times on the server. Better to do it like this:

$.post('linha.html', function (html) 
{
    for(var i=0; i<4; i++) {
        $('#baseTorrent').append(html);
    }
});
  • Thanks for the help

  • Lucas, look at my add-on

  • Thanks for the tip on the :D add-on

Browser other questions tagged

You are not signed in. Login or sign up in order to post.