Bug Tracker

Opened 12 years ago

Closed 12 years ago

Last modified 11 years ago

#7816 closed bug (invalid)

.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:


the following does not work:

<div id="container">
   <div id="hello">Hello</div>
   <div id="goodbye">Goodbye</div>

it does not remove the div as expected

Change History (7)

comment:1 Changed 12 years ago by jitter

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

comment:2 Changed 12 years ago by anonymous

This should be documented better.

comment:3 Changed 12 years ago by ajpiano

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.
Version 0, edited 12 years ago by ajpiano (next)

comment:4 Changed 12 years ago by anonymous

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."

comment:5 Changed 11 years ago by Csmith

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.

comment:6 Changed 11 years ago by Rick Waldron

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

comment:7 Changed 11 years ago by [email protected]

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(); )

Note: See TracTickets for help on using tickets.