0

I have a list of nodes:

<div id="node-1"></div>
<div id="node-2" class="current"></div>
<div id="node-3"></div>
<div id="node-4"></div>
<div id="node-5"></div>

How can I with Zepto get all nodes 3-5, when using $(".current") as selector (node-2)?

2 Answers2

6

This should work. Almost like http://api.jquery.com/nextAll/ and http://api.jquery.com/prevAll/

;(function($){
var e = {
    nextAll: function(s) {
        var $els = $(), $el = this.next()
        while( $el.length ) {
            if(typeof s === 'undefined' || $el.is(s)) $els = $els.add($el)
            $el = $el.next()
        }
        return $els
    },
    prevAll: function(s) {
        var $els = $(), $el = this.prev()
        while( $el.length ) {
            if(typeof s === 'undefined' || $el.is(s)) $els = $els.add($el)
            $el = $el.prev()
        }
        return $els
    }
}

$.extend( $.fn, e )
})(Zepto);
mjlescano
  • 847
  • 9
  • 13
0

Is there something like nextAll() in Zepto.js?

Not according to the documentation, which has a notable gap after next and before not.

This would suggest you'll need a loop, e.g.:

var $current = $(".current"),
    $walk,
    $following = $(),
    $next;

for ($walk = $current.next(); $walk[0]; $walk = $walk.next()) {
    $following.add($walk);
}

That use of add would work with jQuery. Zepto's docs claim that "APIs provided match their jQuery counterparts" (their boldface), but the add docs only talk about using a selector, so you may have to play with that a bit.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875