Skip to content Skip to sidebar Skip to footer

Is There Any Jquery Selector To Support Round Robin?

Is there an easy way to select next() child in JQuery so that if current is last then next should be the first sibling?

Solution 1:

You can use eq on parent element.

var len = $('#mySelector .child').length;
$('#mySelector .child').eq(i % len)....

HTML

<divid="mySelector"><spanclass="child"></span><spanclass="child"></span><spanclass="child"></span><spanclass="child"></span><spanclass="child"></span></div>

Demo: http://jsfiddle.net/tusharj/88n9w98e/

Solution 2:

Short answer, no. However, you can check if next() returns anything and if not retrieve the first sibling. Something like this:

var$next = $el.next()
if ($next.length == 0)
    $next = $el.siblings().first();

Example fiddle

Solution 3:

Simply put:

// if current is the current DOMElement
next = current.nextSibling || current.parentNode.firstChild;

No jQuery needed, these are all DOM features which execute faster than any library implementation.

Post a Comment for "Is There Any Jquery Selector To Support Round Robin?"