cimak
cimak

Reputation: 2021

Find next element, although the element has other parent (but common ancestor)?

How to get the next day after .selected?

$('day.selected').next('day')

works only for days inside the same week.

<calendar>
    <month>
        <week>
            <day/>
            <day class='selected'/>
        </week>

        <week>
            <day/>   //How to get this?
            <day/>
        </week>
    </month>

    <month>
    .
    .
</calendar>

Upvotes: 1

Views: 65

Answers (3)

isherwood
isherwood

Reputation: 61063

To handle all possible scenarios, something like this could be done:

var curDay = $('.selected');
var nextDay;

if ( curDay.next('day') ) {
    nextDay = curDay.next('day');
} else if ( curDay.closest('week').find('day') ) {
    nextDay = curDay.closest('week').find('day:first-child');
} else if ( curDay.closest('month').siblings('month').find('week day') ) {
    nextDay = curDay.closest('month').next('month')
        .find('week:first-child day:first-child')
}

Arun's answer is better, though.

Upvotes: 1

Arun P Johny
Arun P Johny

Reputation: 388316

As a general solution to find the next day element(irrespective of the parent element)

var idx = $('day.selected').index('day'),
    $next = $('day').eq(idx + 1);

Demo: Fiddle, also see this

Upvotes: 6

Felix
Felix

Reputation: 38102

Try this:

$('.selected').parent().next().find(':first-child')

Upvotes: 2

Related Questions