public
Description: various snippets and stuff
Homepage:
Clone URL: git://github.com/localhost/various.git
various / jQuery.ext.custom-siblings.js
100644 43 lines (37 sloc) 1.191 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
(function() {
 
  /*
* custom siblings - last change: 2008-11-29
* copyright 2009 Alex Brem <ab@alexbrem.net>
* license: http://www.opensource.org/licenses/mit-license.php
*/
 
  jQuery.extend({
    siblingsWhile: function(elem, dir, fn, args) {
      var matched = [];
 
      for (var n = elem[dir]; n && n != document; n = n[dir]) {
        if (n.nodeType == 1) {
          if (!fn.call(n, elem, args)) break;
          matched.push(n);
        }
      }
 
      return matched;
    }
  });
 
  jQuery.fn.extend({
    nextAllWhile: function(selector) {
      var ret = jQuery.map(this, function(elem, i) {
        return jQuery.siblingsWhile(elem, 'nextSibling', (jQuery.isFunction(selector) ? selector : function(elem) {
          return $(this).is(selector) })
        );
      });
      return this.pushStack(jQuery.unique(ret));
    },
    prevAllWhile: function(selector) {
      var ret = jQuery.map(this, function(elem, i) {
        return jQuery.siblingsWhile(elem, 'previousSibling', (jQuery.isFunction(selector) ? selector : function(elem) {
          return $(this).is(selector) })
        );
      });
      return this.pushStack(jQuery.unique(ret));
    }
  });
 
})();