I want to make the following list from an array returned from an ajax call.
<ul id="list">
<li><input type="checkbox" name="rss" value="231" />bla</li>
<li><input type="checkbox" name="rss" value="321" checked="checked" />ble</li>
<li><input type="checkbox" name="rss" value="431" />abc</li>
</ul>
Both of them will work (maybe I will need to configure, but they are close. Is one method preferable for the other? Is there something best? Thanks
var l=$("#list");
l.html('');
$(data).each(function(){
l.append('<li><input type="checkbox" name="rss" checked="'+((this.selected)?'checked':null)+'" value="'+this.id+'" />'+this.channel+'</li>');
l.append($("#rss-clone").clone(true).removeAttr('id').find('input').val(this.id).attr('checked',(this.selected)?'checked':null).parent().text(this.channel));
},'json');
// only needed to solve the clone:
<ul class="hidden"><li id="rss-clone"><input type="checkbox" name="rss" value="" />bla</li></ul>
source
share