Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting parent's next sibling's children in jQuery

This is an illustration of a part of my DOM:

enter image description here

I want to trigger an effect on div with class des (which is in an odd div) on click event of div with class but (which is in the previous even div)

How can I select des from but?

like image 451
Prakhar Thakur Avatar asked Jan 25 '15 20:01

Prakhar Thakur


2 Answers

Trivially, if this is the clicked .but element:

$(this).parent().next().children('.des')

This does assume that there are no other elements in the way, i.e. that the .even and .odd elements are immediate siblings, and that .but and .des are immediately children of their respective .even and .odd ancestors.

Less efficiently, but guaranteed to work with any arbitrary number of additional (non-matching) elements in the tree would be:

$(this).closest('.even').nextAll('.odd').first().find('.des')
like image 59
Alnitak Avatar answered Oct 18 '22 16:10

Alnitak


It depends on your markup. Something like this should work, though:

$('.but').on('click', function () {
  $(this).closest('.even').next().find('.des');
});
like image 39
Josh Crozier Avatar answered Oct 18 '22 16:10

Josh Crozier