i have the following code:
$('#test')
.append($.map(this.items, function (item, index) {
return '<li><input type="checkbox" value="' + item.key + '">' item.title + '</li>'
}).join(''));
but i would like it to look more like this:
$('#test')
.append($.map(this.items, function (item, index) {
return $('<li/>').append('<input/>', {type: 'checkbox', value: item.key}).text(item.title)
});
but this dosent seem to work.
anyone can help?
EDIT
since it seems that the answers cannot apply in the RL situation (i made the code short to explain) here is my actual code:
_RenderCheckbox: function () {
$('<ul/>')
.appendTo(this.element)
.addClass('widget')
.append($('<input>').attr({ type: 'text', class: 'field', 'data-map': this.element.attr("data-map") }))
.append($.map(this.items, function (item, index) {
return '<li><label><input type="checkbox" value="' + item.key + '">' + item.parent + item.title + '</label></li>'
}).join(''));
},
this is part of a jquery ui widget i am developing.
.text()replaces the contents of your<li>. You’ll have toappend/prependyour element:Also, don’t use
join(). Just put this code in$.each:And if you prefer huge one-liners: