Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create list in HTML dynamically?

In my jQuery mobile app, I want to display the result from a web service in a list. How do I create the list dynamically?

like image 878
selladurai Avatar asked Mar 15 '11 06:03

selladurai


2 Answers

var arr = ["list", "items", "here"];
$("div").append("<ul></ul>");
for(var i in arr) {
    var li = "<li>";
    $("ul").append(li.concat(arr[i]))
}
like image 102
Erik Sandberg Avatar answered Oct 12 '22 22:10

Erik Sandberg


Better yet,

$.each(
    a ,
    function(i,v) {
        $("#target_id").append("<li>" + v + "</li>") ;
    }
) ;

Where a is an Array of Objects for the list content, i is the index variable passed to the callback function by jQuery.each ($.each) and vis the value for that index.


For reference: http://api.jquery.com/jQuery.each/ .

like image 23
FK82 Avatar answered Oct 13 '22 00:10

FK82