Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the next element that is not immediate?

Tags:

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?)

like image 977
Logan Serman Avatar asked Jan 14 '09 02:01

Logan Serman


People also ask

What is .next in jQuery?

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.

What is find in jQuery?

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.


1 Answers

Similar to the marked answer but looks a little cleaner using the :first selector:

$('.counter').nextAll('span:first') 
like image 185
Nick Olsen Avatar answered Sep 28 '22 05:09

Nick Olsen