$.each(data, function (i, item) {
$('ul').append('<li><a href="yourlink?id=' + item.UserID + '">' + item.Username + '</a></li>');
});
RaYell
2009-07-30 18:34:44
$.each(data, function (i, item) {
$('ul').append('<li><a href="yourlink?id=' + item.UserID + '">' + item.Username + '</a></li>');
});
$.each(data, function(i, item) {
var li = $("<li><a></a></li>");
$("#yourul").append(li);
$("a",li).text(item.Username);
$("a",li).attr("href", "http://...." + item.UserID);
}
Get the <ul>
using jQuery selector syntax and then call append
:
$("ul#theList").append("<li><a href='url-here'>Link Text</a></li>");
See jQuery docs for more information.
The most efficient way is to create an array and append to the dom once.
You can make it better still by losing all the string concat from the string. Either push multiple times to the array or build the string using += and then push but it becomes a bit harder to read for some.
Also you can wrap all the items in a parent element (in this case the ul) and append that to the container for best performance. Just push the '<ul>'
and '</ul>'
before and after the each and append to a div.
var items = [];
$.each(data, function(i, item) {
items.push('<li><a href="yourlink?id=' + item.UserID + '">' + item.Username + '</a></li>');
}
$('#yourUl').append( items.join() );