when I append an element and afterwards remove it the object still exists?
bg = $('<div class="dialog_bg"></div>');
$('#'+elm).append(bg);
bg.remove();
how is that? isn't it possible to remove the element permanently?
when I append an element and afterwards remove it the object still exists?
bg = $('<div class="dialog_bg"></div>');
$('#'+elm).append(bg);
bg.remove();
how is that? isn't it possible to remove the element permanently?
The remove
method just takes the object out of the DOM, in other words, takes it off the page. Are you asking about deleting the object from memory? In that case, I don't think you can do it yourself explicitly - why do you need to? Javascript is a garbage collected language.
edit: also see this question for more information.
so the element is removed from the DOM completely. That's fine. Your question is how to ensure that the element is really removed.
I'd use the .parent() method for that. Because if the element is removed from the DOM it won't have a parent anymore. This may be faster than $("html").has(bg)
because it doesn't have to traverse the whole 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's no way to access the element anymore
bg = null;
How about:
$('div.dialog_bg').remove();
You might have added something inside the div so it's not recognizing it anymore.