Using jQuery to return next list item in a filtered list
jquery, traversal
Solution
You want to use nextAll(). Next is only going to retrieve the sibling immediately following the current element. NextAll() will consider all of the following siblings.
var nextItemWithItemID = $('#item2').nextAll('li[id^=item]:first');
Problem
I have the following list ``` <ul> <li id="item1">Item 1</li> <li id="item2">Item 2</li> <li>Item 3</li> <li id="item4">Item 4</li> </ul> ``` Using jQuery, I'm trying to traverse the list of LIs whose IDs start with "item". ``` var nextItemWithItemID = $("#item2").next("li[id^='item']); ``` However, when I run this code, I end up retrieving "Item 3" rather than "Item 4". How can I get jQuery to get the proper next item from a filtered list of LIs?