Side navigation
#8263 closed feature (worksforme)
Opened February 13, 2011 09:04PM UTC
Closed February 13, 2011 11:10PM UTC
Last modified February 21, 2011 09:43PM UTC
closestDown
Reported by: | uli.riehm@metadea.de | Owned by: | |
---|---|---|---|
Priority: | low | Milestone: | 1.next |
Component: | traversing | Version: | 1.5 |
Keywords: | needsreview | Cc: | |
Blocked by: | Blocking: |
Description
Had a nice idea for an easy core extension. closestDown is opposite to closest, it returns a list of selector matching descendants, except of nested descendants that may also match the selector.
Example: http://jsfiddle.net/uGR2a/ .
(function( $ ) { $.fn.closestDown = function(selector) { var selection = []; var query = function () { this.children().each(function() { if ($(this).is(selector)) { selection.push(this); } else { query.call(this); } }); }; query.call(this); return this.pushStack(selection, "closestDown", selector); }; })(jQuery);
Attachments (0)
Change History (10)
Changed February 13, 2011 11:10PM UTC by comment:1
component: | unfiled → traversing |
---|---|
priority: | undecided → low |
resolution: | → worksforme |
status: | new → closed |
Changed February 13, 2011 11:26PM UTC by comment:2
That idea wasn't thought that easy. Have made a bad example.
Now there is a box, an innerbox and an innerinnerbox (all are class mybox). The box has a child div, there in is innerbox and therin is the innerinnerbox. So it's not that directly a child but it's descendant. The closestDown of box is innerbox. The innerinnerbox is instead not a closestDown.
So it's more complex as children() but also finer than a descendant selector.
Changed February 13, 2011 11:28PM UTC by comment:3
Changed February 13, 2011 11:49PM UTC by comment:4
nicer visualization of closestDown: http://jsfiddle.net/uGR2a/4/
updated code (made a mistake above):
(function($) { $.fn.closestDown = function(selector) { var selection = []; var query = function() { this.children().each(function() { var $this = $(this); if ($this.is(selector)) { selection.push(this); } else { query.call($this); } }); }; query.call(this); return this.pushStack(selection, "closestDown", selector); }; })(jQuery);
Changed February 13, 2011 11:52PM UTC by comment:5
update again, to see that innerbox isn't child of box:
Changed February 14, 2011 01:13AM UTC by comment:6
Ahh.. yes I missed that your code actually does something a bit different (the if/else part).
Still this doesn't behave similar to .closest()
which
- starts matching with itself
- only returns zero/one element
- only returns the closest (parent) element matching the selector
while your code
- doesn't start matching with itself
- returns zero/multiple descendant elements
- the meaning of closest only applies partially, as elements of different nesting level are considered closest by your code test case
So the name doesn't apply to well, it would be difficult to explain which elements the method really matches thus I still think this code is better suited as a plugin.
Changed February 14, 2011 12:23PM UTC by comment:7
You don't want it huh?
"as elements of different nesting level are considered closest by your code"
Right! It matches elements by a selector, without matching nested-selector-matching elements.
"doesn't start matching with itself"
No, that makes no sense for the use case.
Changed February 14, 2011 01:59PM UTC by comment:8
keywords: | → needsreview |
---|
Replying to [comment:7 anonymous]:
You don't want it huh?
??
Changed February 21, 2011 08:30PM UTC by comment:9
I like this a lot. +1
Changed February 21, 2011 09:43PM UTC by comment:10
Replying to [comment:9 anonymous]:
I like this a lot. +1
Nice to hear.
Have decided to name my function 'closestDesc'. That's a more meaningful short for 'closest descendants'.
Function Description:
"In opposite to closest (ancestor), closestDesc returns the closest descendants matching the selector."
Because of the nature of xml-like markup there is always one ancestor, but many descendants.
Thanks for taking the time to contribute to the jQuery project by writing a feature request. What you describe sounds more like the opposite to
.parents()
then of.closest()
.Anyway, this functionality is already available as
.children()
(the same method you reuse in your code) already supports the.children( [ selector ] )
signature.Check http://api.jquery.com/children and your test case rewritten to use
.children( selector )