jQuery empty() vs remove()

mabuzer picture mabuzer · Jun 22, 2010 · Viewed 51.1k times · Source

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

nickf picture nickf · Jun 22, 2010
  • empty() will empty the selection of its contents, but preserve the selection itself.
  • remove() will empty the selection of its contents and remove the selection itself.

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.


Here are links to documentation, which also contains examples: