Jquery Select element 2 positions further - another way to .next().next()
I am searching for a way how I could select a div element which is not the direct next one to the one which is "selected" by a click function.
<div id="click">(siblings)</div><div>text</div><div id="get this one"></div>
Now I would like to select the one with the id "get this one" - in my code this id is not available. All the divs hav开发者_JAVA技巧e the same class and do have siblings.
I could select the third one by$(this).next().next()
but I think it's not the best way to do it.
Also there can be div
s before the one which is clicked - so it's not necessarily the first one.
I tried the :nth-child
selector but didn't find a solution.
Thanks for your help,
PhilYou can use .nextAll()
with .eq()
for your dynamic approach, like this:
$(this).nextAll().eq(1) //0 based index, this would be .next().next()
This would allow you to get n
siblings forward, which seems to be what you're after.
It seems that $(this).parent().find('div').eq(2).attr('id')
should work.
UPDATE( Added find('div') )
精彩评论