'); $...">

Removing an item in jQuery

when i add an element and then delete it, does the object still exist?

bg = $('<div class="dialog_bg"></div>');
$('#'+elm).append(bg);

bg.remove();

like this? Can’t I permanently delete an item?

+3
source share
3 answers

so that the item is completely removed from the DOM. It's great. Your question is how to make sure that the item is really deleted.

I would use the .parent () method for this. Because if an item is removed from the DOM, it will no longer have a parent. This may be faster than $("html").has(bg)because it does not need to go through the entire DOM tree.

bg = $('<div class="dialog_bg"></div>');
$('#'+elm).append(bg);

bg.remove();

if(bg.parent().length == 0) {
   // removed succesfully
} else {
   // still somewhere in the dom
}

// tells the garbage collector to free the memory because there no way to access the element anymore
bg = null;
+2
source

remove DOM, , . ? , - ? Javascript - .

edit: . .

+2

What about:

$('div.dialog_bg').remove();

You may have added something inside the div so that you no longer recognize it.

-1
source

All Articles