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?.
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()
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.
The nextUntil() method returns all next sibling elements between two given arguments.
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.
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
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