javascript - How to select nth next data element starting from current? -
i have html structure so:
<div data-month="january"></div> <div data-month="february"></div> <div data-month="march"></div> <div data-month="april"></div>
but there lot of html structure around divs.
now, know how select data month, e.g.: $('[data-month=february])
gets jquery object div data-month of february.
now, imagine have selected february div above, there way nth neighbour of 'february' (the div n-positions right)?
i tried using eq()
function, cannot start current object. (i need start specific data-month , don't know index upfront.)
simple! use .next() twice so:
$("[data-month='february']").next().next()
more info here
a more general approach use index()
find current item's index, add offset , use parent().children().eq()
so:
var current = $("[data-month='february']"); var other = current.parent().children().eq(current.index() + offset);
Comments
Post a Comment