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?
-
can u suggest some other examples?selladurai– selladurai2011-03-15 06:28:58 +00:00Commented Mar 15, 2011 at 6:28
-
1refer stackoverflow.com/questions/5048360/… and stackoverflow.com/questions/4039428/…Akshatha– Akshatha2011-03-15 06:30:36 +00:00Commented Mar 15, 2011 at 6:30
Add a comment
|
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]))
}
2 Comments
selladurai
In that list, how can I get the selected value?
Jesse Chisholm
In this list, there is no "selected" value. It is a display only list. Build it with select and option instead of ul and li if you want a selection list.
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/ .
2 Comments
Jeff Dickey
I wouldn't call that "better"; the accepted answer is pure JS, without any dependencies like jQuery (which is needed less and less as JavaScript continues to mature).
FK82
@JeffDickey First of all, the accepted answer also uses jQuery (note the
$("div").append(...)). I also hope you realize that this post is from 2011 and that OP explicitely states that he's using jQuery. Just sayin' :D