Opened 15 years ago
Closed 15 years ago
#2047 closed enhancement (worksforme)
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); } });
Change History (5)
comment:1 Changed 15 years ago by
Resolution: | → worksforme |
---|---|
Status: | new → closed |
comment:2 Changed 15 years ago by
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.
comment:3 Changed 15 years ago by
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
comment:4 Changed 15 years ago by
Resolution: | fixed |
---|---|
Status: | closed → reopened |
comment:5 Changed 15 years ago by
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.