fernacolo
fernacolo

Reputation: 7439

jQuery: memory management for new, temporary DOM elements

Suppose I create an element using $('some html'), like this:

var imgSrc = determineImageURL();
var myImg = $('<img>', { src: imgSrc })[0]; // Creates a new <img>.

My question is:

Upvotes: 0

Views: 692

Answers (1)

rogerz
rogerz

Reputation: 1071

The parent of myImg depends on where you append it to. Given only the code you quoted, it is none.

The new element is garbage collected when the function returns, if you don't attach the node to a parent in the function.

The variable declare with var only survive in function scope, unless you creates other reference to it (attached to a parent or referred in closure).

Upvotes: 3

Related Questions