I want to find the first span element after class counter, in code something like this:
<div class="counter"></div> <p></p> <span></span>
It seems like the next() function only finds the immediate next element, so something like this:
$(".counter").next("span")
Won't work. The way I have been using is a bit lengthy and I was wondering if there was a shorter way, it is this:
$(".counter").nextAll("span").eq(0)
I think the closest()
method in jQuery 3 will do the trick, but I am using 1.2.6 -- is there a better way to do this (am I just using next()
wrong?)
The next() method returns the next sibling element of the selected element. Sibling elements are elements that share the same parent. The DOM tree: This method traverse forward along the next sibling of DOM elements.
jQuery find() Method The find() method returns descendant elements of the selected element. A descendant is a child, grandchild, great-grandchild, and so on. The DOM tree: This method traverse downwards along descendants of DOM elements, all the way down to the last descendant.
Similar to the marked answer but looks a little cleaner using the :first selector:
$('.counter').nextAll('span:first')
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