Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery .next(); target the next following second or third element

Tags:

jquery

We know that the JQuery function of .next() will help us find out the next following element but i want to know that, how we can ask JQuery .next() help us to find out the next's next element or even next's next's next element?

I blindly try on $(this).next().next().val() and it works, it able to get value of next 2nd element. so if code like this, $(this).next().next().next().next().next().val() it get 5th element.

I think it should have better way to code it, so does anyone know?

It will be funny if i want to get next 100 element attribute! so i know you guys will ask what kind of programming or usage need to code like this?.

like image 469
user1493339 Avatar asked Aug 08 '12 05:08

user1493339


People also ask

What is next() in jQuery?

The next() is an inbuilt function in jQuery which is used to return the next sibling of the selected element. Siblings are those having same parent element in DOM Tree. Document Object Model (DOM) is a World Wide Web Consortium standard. This defines for accessing elements in the DOM tree. Syntax: $(selector).next()

How to get next element of an element jQuery?

jQuery next() Method 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.

Which method returns all next sibling elements between two given arguments?

The nextUntil() method returns all next sibling elements between two given arguments.

How can I get second child in jQuery?

Using jQuery :nth-child Selector You have put the position of an element as its argument which is 2 as you want to select the second li element. If you want to get the exact element, you have to specify the index value of the item. A list element starts with an index 0.


1 Answers

Use your imagination :

var elem = ​$("#elementID")​; //gets the start element
elem.siblings().eq(elem.index()+8).css('color', 'red');// ninth next element

Start with an element, get all it's siblings and filter with eq() based on the start elements index plus however many sibling elements you wish to skip.

FIDDLE

like image 86
adeneo Avatar answered Nov 09 '22 17:11

adeneo