How to create a list in HTML dynamically?

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

+6
javascript jquery html jquery-mobile
source share
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])) } 
+14
source share

Even better,

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

Where a is an array of objects for the contents of the list, i is the index variable passed to the jQuery.each callback function ( $.each ), and v is the value of this index.


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

+15
source share

All Articles