Jquery remove command clarifications

Hi,

I am reading the book jQuery in Action - Bibeault, Katz, based on jQuery 1.2 on the paragraph about remove() command.

It’s clear that this command removes all elements in the wrapped set from the page DOM, but I don’t understand what returns, (and it seems that on the remove() command page on the jquery website there aren’t any additional explanations about it). On the book, I am arrived to read the following, but I don’t understand its meaning:

as with many other jQuery commands, the wrapped set is returned as the result of this command. The elements that were removed from the DOM are still referenced by this set (and hence not yet eligible for garbage collection) and can be further operated upon using other jQuery commands including appendTo(), prependTo(), insertBefore(), insertAfter() and any other similar behaviours we’d like.

does it mean that the removed element is still available? and in what way? Why on the site, there isn’t any of what I cited? Can you give me any clarifications with examples? Many thanks!

Most jQuery methods return the jQuery object they operated on, which is why they are chainable, such as

$('.foo').show().addClass('bar');

The same works with .remove() – consider this markup

<span class="foo">Hi there!</span>
<div></div>
<div></div>

and run

$('.foo').remove().appendTo('div');

which will result in

<div>
  <span class="foo">Hi there!</span>
</div>
<div>
  <span class="foo">Hi there!</span>
</div>

The jQuery object doesn’t get deleted just because it got removed from the DOM; so you could also store the reference in a variable like

var foo = $('.foo').remove();

// Then, later
foo.appendTo('div');
2 Likes

Ok, many thanks! It’s clear!

What is “Garbage collection”? And what is the meaning of the sentence between parenthesis I cited?

[quote=“franciska, post:1, topic:229987, full:true”]
On the book, I am arrived to read the following, but I don’t understand its meaning:

as with many other jQuery commands, the wrapped set is returned as the result of this command. The elements that were removed from the DOM are still referenced by this set (and hence not yet eligible for garbage collection) and can be further operated upon using other jQuery commands including appendTo(), prependTo(), insertBefore(), insertAfter() and any other similar behaviours we’d like.

does it mean that the removed element is still available?[/quote]

Yes it does.

[quote=“franciska, post:1, topic:229987, full:true”]
and in what way?[/quote]

The selected elements are added to an internal stack, which gets returned as a jQuery object allowing you to chain further actions on to them.

Chapter two takes you through some of those details of the wrapped set.

I thought that there was. You might just not have thought to chase it down.

Go to the .remove() documentation page and you’ll see to the right of .remove( [selector ] ) that it says “Returns: jQuery

Following that link takes you to a page that says things like:

Whenever you use a "destructive" jQuery method that potentially changes the set of elements in the jQuery object, such as .filter() or .find(), that method actually returns a new jQuery object with the resulting elements.

Garbage collection means that objects which are not reachable (either because there’s no reference any more or because the only reference is some sort of cycle) are getting deleted automatically, so that the memory gets freed. So jQuery objects which are getting removed are not getting garbage collected as long as they still have a reachable reference.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.