Insert multiple html with ajax

Asked

Viewed 461 times

0

I have the following code in jquery:

musicas: function(){
    $.ajax({
        url: 'assets/includes/oloco.php',
        type: 'GET',
        dataType: 'json',
        beforeSend: function(){
            efeito.inicio();
        },
        success: function(data){
            $('.texto').html(data[0].musica);
            efeito.fim();
        }
    }),
    setTimeout(function(){
        player.musicas();
    }, 15000);
}

And in html as seen I have:

 <div class="text"></div>

However, my current ajax only changes the text of div, I would like to add 5 Divs changing the date[]! The oloco.php request is in json and has exactly 5 values as you can see below:

[
{
    "musica": "Luan Santana  Acordando o Pr\u00e9dio - Make U Sweat Remix (Lyric Video)"
},
{
    "musica": "Kygo ft. Parson James - Stole the show (Lyrics video)"
},
{
    "musica": "Justin Bieber Ft Daddy Yankee y Luis Fonsi - Pasito a Pasito Suave Suavecito (Letras)"
},
{
    "musica": "Jhef - Vida M\u00edtica (Official V\u00eddeo)"
},
{
    "musica": "Jhef - Pode ser n\u00f3s dois (Official Video)"
}
]

How can I insert multiple Ivs with different values and in order?

1 answer

1


Just iterate on the result of the AJAX request and use the function append jQuery to insert the element into the DOM. It should look like this:

musicas: function(){
    $.ajax({
        url: 'assets/includes/oloco.php',
        type: 'GET',
        dataType: 'json',
        beforeSend: function(){
            efeito.inicio();
        },
        success: function(data){

            // Exclui possíveis músicas anteriores:
            $('.texto').html("");

            // Itera sobre todos os elementos de data:
            $(data).each(i => {
                // Cria um novo elemento p:
                let element = $("<p>");
                // Define o conteúdo de p:
                element.html(data[i].musica);
                // Adiciona o novo elemento ao DOM:
                $('.texto').append(element);
            });

            efeito.fim();
        }
    }),
    setTimeout(function(){
        player.musicas();
    }, 15000);
}
  • And how can I just update in setTimeout instead of adding more Divs?

  • What do you mean? You want a song added every 15 minutes?

  • That way every 15 minutes you add new <p> with the same songs, you know? I would like you to add just by entering the site and then just update the contents of the site...

  • It’s not clear enough yet. You want that, when accessed the page, the 5 songs are inserted and after 15 minutes these songs change to the new ones that return from the AJAX request?

  • It’s like, there are 5 songs, but they update the names every time... With the append are added 5 Divs every 15 seconds, resulting numerous texts... I want from the first time added just update your content (html) instead of continuing to trigger....

  • Every 15 seconds it is inserting new Ivs causing this: http://prnt.sc/f5o0za

  • After entering the site I just want to update the contents of the site (instead of adding new ones with the append)...

  • Roger. I edited the answer to fix this.

  • Thank you so much for your attention, friend, you’re a show.

  • I believe that this code will already do that, will update without adding. This snippet ensures this: // Deletes possible previous songs: $('.text'). html("");

Show 5 more comments

Browser other questions tagged

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