简体   繁体   中英

Generate multiple div in jquery loop

I need to ask for help. Having this code:

    $(".apprezzamenti").click(function()
    {    
        var id = $(this).attr('id');

        $.get("http://localhost/laravel/public/index.php/apprezzamenti_modal/"+id, 
        function(data)
        { 
            data_parsed = JSON.parse(data);

            // BELOW is the loop that I would like to generate multiple div using .append()

            for(var i=0; i<data_parsed.length; i++)
            {
                var html = '<img src="images/provvisorie/immagine-profilo.png" width="30px" and height="30x" />';
                    html += ' ';
                    html += "<a href='http://localhost/laravel/public/index.php/utente/" + data_parsed[i].id_user + "' style='font-weight: bold;'>" + data_parsed[i].username + "</a>";

                $(".modal-body-apprezzamenti>p").append(html).append('.modal-body-apprezzamenti>p').append($('<br/>'));  
            } 

            $(".chiudi-popup").click(function()
            {
                $(".modal-body-apprezzamenti>p").html('');
            });

            $('#main').click(function() 
            {
                $(".modal-body-apprezzamenti>p").html('');
            });        
        }); 
    });

I'd like to create a div for each looping. How could I do? Thanks for your help.

With jQuery it's very easy, look at this JSFiddle http://jsfiddle.net/Ldh9beLn/

I use the function jQuery appendTo() to insert each div inside another div with id = "inserts" if you don't understand a section of this code or how to adapt to yours leave me a comment.

var things = ["apple", "house", "cloud"];

for (var i = 0; i < things.length; i++) {
  $("<div>"+things[i]+"</div>").appendTo("#inserts");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM