How to clone element with new id?
I have some elements on the page. Here is example of one element:
<div id="119" class="message">
<div class="messageAuthor">iggy</div>
<div class="messageBody messageMessage">hello</div>
<div class="messageDate messageCreate_date">12:13</div>
</div>
I get from server new items and items which were updated in JSON. Here is example of using JSON response:
$.each(ajaxResult.items, function(index) {
for (var key in ajaxResult.items[index]) {
if (ajaxResult.items[index].hasOwnProperty(key)) {
if ($('.'+ajaxResult.controller + self.toUpperFirst(key)).length) {
if ($('#'+ajaxResult.items[index].id).length) {
$('#'+ajaxResult.items[index].id).children('.'+ajaxResult.controller + self.toUpperFirst(key)).text(ajaxResult.items[index][key]);
}
else {
var prevIndex = parseInt(index) - 1;
$('#'+ajaxResult.items[prevIndex].id).after($('#'+ajaxResult.i开发者_运维知识库tems[prevIndex].id).html());
}
}
}
}
});
I want to add new elements after existing elements. I don't know what the best way to do it. I need help.
jQuery(element).clone().attr('id',new_id).appendTo(parent_element);
精彩评论