Side navigation
#2047 closed enhancement (worksforme)
Opened December 13, 2007 12:40PM UTC
Closed December 17, 2007 02:34PM UTC
better parent function (Prototype "up" like)
Reported by: | phpmyforum | Owned by: | |
---|---|---|---|
Priority: | minor | Milestone: | 1.2.2 |
Component: | core | Version: | 1.2.1 |
Keywords: | Cc: | ||
Blocked by: | Blocking: |
Description
I often need a specific parent element instead of all or the direct parent, so I came up with this function:
$.fn.extend({ //### Prototype "up" method up: function(selector) { var found = ""; selector = $.trim(selector || ""); $(this).parents().each(function() { if (selector.length == 0 || $(this).is(selector)) { found = this; return false; } }); return $(found); } });
Attachments (0)
Change History (5)
Changed December 17, 2007 01:01AM UTC by comment:1
resolution: | → worksforme |
---|---|
status: | new → closed |
Changed December 17, 2007 09:56AM UTC by comment:2
resolution: | worksforme |
---|---|
status: | closed → reopened |
This is the difference:
$(this).parents(selector);
-> returns a set of all ancestors
$(this).up(selector);
-> returns just the first matched element
It behaves similar to prototype's up method (http://prototypejs.org/api/element/up), except you can't pass a number.
Though this could also be added.
Changed December 17, 2007 02:34PM UTC by comment:3
resolution: | → fixed |
---|---|
status: | reopened → closed |
Just add the :first selector to your selector. Then it will return what you want. You can also use :eq, :gt or :lt.
Here are the docs for the selectors: http://docs.jquery.com/Selectors
Changed December 17, 2007 02:34PM UTC by comment:4
resolution: | fixed |
---|---|
status: | closed → reopened |
Changed December 17, 2007 02:34PM UTC by comment:5
resolution: | → worksforme |
---|---|
status: | reopened → closed |
Is this different from :
$(this).parents(selector);
?
http://docs.jquery.com/Traversing/parents#expr
If I'm missing something about how yours differs please feel free to reopen the ticket and let me know.