I'm given a jQuery object $myObject and a DOM element myElement contained in $myObject.
How can I find the next DOM element contained in $myObject right after myElement?
Pure JavaScript:
var nextElement = myElement.nextSibling;
Edit: Reading some of your comments, it looks like I may have misunderstood what you are looking for. You mention that your elements are added to $myObject using .add(). It sounds like you are trying to access the elements in the order they were added to $myObject. Is that right? Elements are stored in a jQuery object in the order they appear in the DOM. The order they are added to the jQuery object is not preserved. You can get the next element in the set of matched elements using .index() to find the position of myElement and then get the next one:
var idx = $myObject.index(myElement);
var nextElement = $myObject[idx + 1];
But the next element will be according to DOM order, not the order the elements were added. To get that, you'd need to store the order yourself.
See this demo: http://jsfiddle.net/gilly3/Z96Gd/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With