I\'m fairly new to jquery and struggling with something that should be fairly simple. I want to select the previous and next div with class \"MenuItem\" from the div with class
Have you tried:
$('div.MenuItemSelected').nextAll('.MenuItem:first');
I think the problem you are facing is that next
returns the very next sibling, whereas nextAll
returns a collection of all the subsequent siblings matching your selector. Applying the :first selector to your nextAll
filter should make things right.
I would like to point out, that if the structure of your markup is consistent (so it's always guaranteed to work), then your current solution might (benchmark, benchmark, benchmark) well be the faster way:
$('div.MenuItemSelected').next().next();