I have a jQuery selector of arbitrary complexity:
I have a single item descriptor inside this list:
What is the most efficient way to find before and after links in a list ? Keep in mind that the selector is active and that entries can be added or removed between getting current and trying to find the next or previous link.
The best I can come up with is to bypass the O (n) list :
function adjacentInList(list, target, direction) { var delta = (direction === 'prev' ? -1 : 1); list = $(list); target = $(target); var result = null; list.each(function(index, element) { if (target[0] === element) { result = list.slice(index + delta, index + delta + 1); } }); return result; }
source share