selladurai
selladurai

Reputation: 6779

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?

Upvotes: 10

Views: 39789

Answers (2)

FK82
FK82

Reputation: 5075

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/ .

Upvotes: 17

Erik Sandberg
Erik Sandberg

Reputation: 486

var arr = ["list", "items", "here"];
$("div").append("<ul></ul>");
for(var i in arr) {
    var li = "<li>";
    $("ul").append(li.concat(arr[i]))
}

Upvotes: 20

Related Questions