I'm not sure about a bug, but the easiest way I could see to accomplish this is:
var el = $('.close');
$('.modal').empty().append(el);
JS Fiddle demo.
A slightly better approach (on the incorrect assumption of multiple .close
elements):
$('.modal').each(
function(){
var that = $(this),
el = that.find('.close');
that.empty().append(el)
});
JS Fiddle demo.
A combination of using jQuery each()
and a plain-JavaScript function:
function fratricide(el) {
while (el.previousSibling) {
el.parentNode.removeChild(el.previousSibling);
}
while (el.nextSibling) {
el.parentNode.removeChild(el.nextSibling);
}
}
$('.close').each(
function() {
fratricide(this);
});
JS Fiddle demo.
And a JS Perf comparison of approaches. Unsurprisingly the use of plain-JavaScript speeds things up appreciably, even though it is, I think, pretty ugly to read (though I'm unsure how much prettier it could realistically become).
Interestingly (to me, anyway, now that I've updated the previously-linked JS Perf) using a variable to store the reference to el.parentNode
makes negligible difference, presumably because despite saving a reference to the element/node the DOM is still having to be accessed in order to find the el.previousSibling
node?