-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.untilFound.js
50 lines (48 loc) · 1.14 KB
/
jquery.untilFound.js
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
44
45
46
47
48
49
50
/*
* nextUntilFound() / prevUntilFound()
* loops through the siblings until right one found
*/
(function( $ ) {
$.fn.nextUntilFound = function(selector){
if(selector == null) return this.next();
var lastFound = this;
var siblings = this.siblings(selector);
if(siblings.length == 0) return siblings;
while(true){
var next = lastFound.next();
if( next.is(selector) ){
return next;
}else if(next.length == 0){
// return first of the siblings
return siblings.first();
}else if(lastFound == next){
// not found
return $();
}else{
// search again
lastFound = next;
}
}
}
$.fn.prevUntilFound = function(selector){
if(selector == null) return this.prev();
var lastFound = this;
var siblings = this.siblings(selector);
if(siblings.length == 0) return siblings;
while(true){
var prev = lastFound.prev();
if( prev.is(selector) ){
return prev;
}else if(prev.length == 0){
// return last of the siblings
return siblings.last();
}else if(lastFound == prev){
// not found
return $();
}else{
// search again
lastFound = prev;
}
}
};
}(jQuery)