Interaction between XMLDocument and JQuery .append()

On one of my .js files I've just noticed a weird interaction between the 2 elements I mention on the title.

I'm first going to provide sample code and after that I will address the issue in more detail.

var xmlClone = xmlCreateDocFromString(this.xmlTemplate);
$(conf.current[currentTab]).find('#' + refID).after($(xmlClone).children());

Where the definition of xmlCreateDocFromString is:

function xmlCreateDocFromString(str)
{
var xmlDoc;

if ( window.DOMParser )
{
    var parser = new DOMParser();
    xmlDoc = parser.parseFromString(str, "text/xml");
}
else
{
    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async = false;
    xmlDoc.loadXML(str);
}

return xmlDoc;

}

I'm testing this on Chrome so just pay attention to the "if" block in there.

The issue is, after the first line in the first code block, xmlClone does have the contents of this.xmlTemplate, but after using the ".after()" JQuery function (or .before, or .append) xmlClone just becomes and empty xmlDoc and that is really screwing up my code.

So, what I need is a way to append the contents of xmlClone AND keep them in xmlClone at the same time (aka what appends normally does for a JS object).

If you need any additional information please ask and I will answer ASAP

Upvotes: 0

Views: 150

Answers (1)

Billy Moon
Billy Moon

Reputation: 58531

jQuery tends to move objects rather than copy them as it is generally what people are looking to do.

I am sure there are other ways, but you could clone the object before appending...

$(conf.current[currentTab]).find('#' + refID).after($(xmlClone).clone().children());

Upvotes: 1

Related Questions