Skip to main content

Bug Tracker

Side navigation

#7816 closed bug (invalid)

Opened December 21, 2010 02:49PM UTC

Closed December 21, 2010 11:21PM UTC

Last modified March 13, 2012 09:13AM UTC

.remove([selector]) not working as expected

Reported by: anonymous Owned by:
Priority: undecided Milestone: 1.6
Component: unfiled Version: 1.4.4
Keywords: Cc:
Blocked by: Blocking:
Description

the following does not work:

<div id="container">
   <div id="hello">Hello</div>
   <div id="goodbye">Goodbye</div>
</div>
$('#container').remove('#hello');

it does not remove the div as expected

Attachments (0)
Change History (7)

Changed December 21, 2010 11:21PM UTC by jitter comment:1

resolution: → invalid
status: newclosed

Thanks for taking the time to contribute to the jQuery project by writing a bug report.

This is not a bug in jQuery but a misunderstanding on how .remove(selector) works. The selector is only applied to the elements contained in the jQuery collection not to the children of those elements. $('#container') contains only one element which doesn't match #hello thus nothing happens. If you e.g. change this to $('div').remove('#hello') the collection contains 3 div's but only the one with id hello gets removed.

test case

Changed December 22, 2010 06:28PM UTC by anonymous comment:2

This should be documented better.

Changed December 22, 2010 07:00PM UTC by ajpiano comment:3

_comment0: I added a mention in one of the examples showing that $("foo").remove("bar") is analogous to $("foo").filter("bar").remove() to reinforce this behaviour. But the docs were pretty clear to begin with: \ \ \ {{{ \ .remove( [ selector ] ) \ selectorA selector expression that filters the set of matched elements to be removed. \ }}} \ 1293044485313982

I added a mention in one of the examples showing that $("foo").remove("bar") is analogous to $("foo").filter("bar").remove() to reinforce this behaviour. But the docs were pretty clear to begin with:


.remove( [ selector ] )

selector A selector expression that filters the set of matched elements to be removed.

Changed August 18, 2011 06:12PM UTC by anonymous comment:4

The documentation explaining what the "selector" parameter does is not clear. In the phrase "filters the set of matched elements", it is not clear what "the set" is referring to, so it can be interpreted in two ways. Does "the set of matched objects" refer to "the [original jQuery object] set of matched objects", or does it refer to "the [newly matched] set of matched objects".

While it's obvious to everyone that this parameter is creating some kind of new set of matched objects, it lacks words like "existing set" or "subset", and even the parameter name "selector" is a bit misleading, since it's "filtering" the existing set, not selecting a new one. It may be better to call it "filter_selector" or include language like "filters a subset of the existing matched set" like "selector: A selector expression that filters, for removal, a subset of the existing matched set of elements."

Changed November 03, 2011 06:36AM UTC by Csmith comment:5

This same issue was killing me for 2 hours. The documentation really is a bit unclear. Once I stumbled across this article I 'got' it.

Changed November 03, 2011 03:42PM UTC by rwaldron comment:6

Through out the docs, "the set" is "the set" of elements currently selected.

Changed March 13, 2012 09:13AM UTC by maggud@gmail.com comment:7

Please improve documentation on this.

I have also struggling with this for hours. It feels like

$('#container').find('#hello');

and

$('#container').remove('#hello');

both should operate in the same way.

There should be an example of how to achieve that result.

(After stubmling upon this ticket I came up with something like this:

$('#container').find('#hello').remove();

)