mabuzer mabuzer - 5 months ago 15
Javascript Question

jQuery empty() vs remove()

What's the difference between

empty()
and
remove()
methods in
jQuery
, and when we call any of these methods, the objects being created will be destroyed and memory released?

Answer
  • empty() will remove all the contents of the selection.
  • remove() will remove the selection and its contents.

Consider:

<div>
    <p><strong>foo</strong></p>
</div>

$('p').empty();  // --> "<div><p></p></div>"

// whereas,
$('p').remove(); // --> "<div></div>"

Both of them remove the DOM objects and should release the memory they take up, yes.