Ticket #4446 (closed enhancement: wontfix)
.andSelf(selector)
| Reported by: | karamfil | Owned by: | |
|---|---|---|---|
| Priority: | low | Milestone: | 1.next |
| Component: | traversing | Version: | 1.3.2 |
| Keywords: | andSelf,filter,1.7-discuss | Cc: | |
| Blocking: | Blocked by: |
Description (last modified by dmethvin) (diff)
Why not andSelf() function to have a selector param. Here is a particular example
$('element').nextAll('.className').andSelf('.className').doSomething();
This way one can add self only if it meets particular requirements.
Here is a fast solution :)
$.fn.andSelf = function(selector){ return (selector == null || this.prevObject.is(selector)) ? this.add( this.prevObject ) : this; }
Change History
comment:3 Changed 3 years ago by scottsb
I would find this feature very useful. It seems like an appropriate change to to bring andSelf() in line with the other traversal methods.
comment:4 Changed 3 years ago by boushley
Assuming the provided code functions I think this would be a reasonable candidate for the next version. I can think of some use cases where this might be handy, although I've never needed it in the past.
comment:5 Changed 3 years ago by SlexAxton
- Keywords filter added
- Priority changed from minor to low
- Status changed from new to open
- Milestone changed from 1.4 to 1.5
comment:7 Changed 2 years ago by gg
I've submitted a pull request which implements this: https://github.com/jquery/jquery/pull/203
comment:10 Changed 2 years ago by john
- Keywords andSelf,filter,1.7-discuss added; andSelf filter removed
Nominating ticket for 1.7 discussion.
comment:11 Changed 2 years ago by dmethvin
+1, I like the feature but maybe we can find something better than .andSelf() to describe it?
comment:13 Changed 2 years ago by jaubourg
-1, I'd like these kind of helpers to be added into an official plugin, not be put in core.
comment:14 Changed 2 years ago by ajpiano
- Description modified (diff)
-1, .andSelf should do what it says.
comment:16 Changed 2 years ago by danheberden
+1, Seems reasonable since most other stack addition functions take a selector
comment:18 Changed 2 years ago by john
- Description modified (diff)
-1, I don't really agree with the API change.
comment:19 Changed 2 years ago by scott.gonzalez
+0, I'm torn on this one. No strong feelings either way.
comment:20 Changed 2 years ago by addyosmani
-1
comment:21 Changed 2 years ago by jzaefferer
+1, useful, and provides consistent optional filtering for traversing methods
comment:22 Changed 2 years ago by cowboy
+1, Doing this would not only be cool, it would make #4596 a non-issue (see my comment there)
comment:23 Changed 23 months ago by dmethvin
- Status changed from open to closed
- Resolution set to wontfix
- Description modified (diff)
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

Watch out there. The .is() method only checks the first element in the jQuery object. I think you'd want .filter() instead. Probably the .andSelf() name isn't clear enough but at this point I guess we're stuck with it. The enhancement seems useful though.