There are two problems with the one-liner mentioned in the other answers:
- They skip the case where there are no invisible lines. In this case, nextUntil does not return any elements. Below is the code that fixes this problem.
- If you use a specific class name, instead of the default show / hide jQuery, then it also does not work reliably.
The code below fixes both of the above questions with other answers:
//invisibleRowClassName parameter is optional function nextVisibleSibling(element, invisibleRowClassName) { var selector = invisibleRowClassName ? (":not(." + invisibleRowClassName + ")") : ".visible"; var invisibleElements = element.nextUntil(selector); if (invisibleElements.length === 0) { return element.next(); } else { return invisibleElements.last().next(); } }
And here is the code to get the previous visible element.
//invisibleRowClassName parameter is optional function prevVisibleSibling(element, invisibleRowClassName) { var selector = invisibleRowClassName ? (":not(." + invisibleRowClassName + ")") : ".visible"; var invisibleElements = element.prevUntil(selector); if (invisibleElements.length === 0) { return element.prev(); } else { return invisibleElements.last().prev(); } }
source share