Ashwin
Ashwin

Reputation: 12421

IE does not retain deleted DOM

I am taking a dom element in a variable and then erasing it. When I append that variable to the dom at some other place it does not appear in all versions of IE. Whereas it works fine in chrome and FF

Fiddle - http://jsfiddle.net/NdtNP/

HTML:

<div id="d1" style="color:red">sdjkfk
    <a href="google.com">Click here</a>
    sjlfkj df
</div>
<div id="d2"></div>

JS:

$(function(){
    var a = $("#d1").children("a");
    $("#d1").html("");
    $("#d2").append(a);
});

How can I avoid this?

Upvotes: 2

Views: 47

Answers (3)

Andrew Bowman
Andrew Bowman

Reputation: 869

Why not try to append before deleting by switching up your script like this

$(function(){
    var a = $("#d1").children("a");
    $("#d2").append(a);
    $("#d1").html("");
});

Upvotes: 1

Use .detach()

$(function () {
    var a = $("#d1").children("a").detach();
    $("#d1").html("");
    $("#d2").append(a);
});

jsfiddle

Upvotes: 3

Travis J
Travis J

Reputation: 82297

jsFiddle Demo

You should use remove. It will return the elements.

$(function(){
 var a = $("#d1").children("a").remove();
 $("#d1").html("");
 $("#d2").append(a);
});

Upvotes: 3

Related Questions