Andy Chuo
Andy Chuo

Reputation: 127

jQuery selector of the element inside the next sibling

Can anyone tell me how to select the 'a' within the 'li' from the class 'active_page'? I tried the following but it does not work:

if($('.active_page').next('.page_link').length==true){
    go_to_page(new_page);
}


<ul>
    <li>
        <a class="previous_link" href="javascript:go_to_previous();">Prev</a>
    </li>
    <li>
        <a class="page_link active_page" href="javascript:go_to_page(0)" longdesc="0">1</a>
    </li>
    <li>
        <a class="page_link" href="javascript:go_to_page(1)" longdesc="1">2</a>
    </li>
    <li>
        <a class="page_link" href="javascript:go_to_page(2)" longdesc="2">3</a>
    </li>
    <li>
        <a class="next_link" href="javascript:go_to_next();">Next</a>
    </li>
</ul>

Thanks

Upvotes: 4

Views: 3464

Answers (3)

T.J. Crowder
T.J. Crowder

Reputation: 1075537

next looks for a sibling, but the a element has no siblings. Its parent li does:

if($('.active_page').parent().next().find('.page_link').length){
    go_to_page(new_page);
}

Also, length is a number, don't compare it to true. If you want to know if length is not 0, the above works.

Upvotes: 1

bipen
bipen

Reputation: 36551

not sure what you are tryin to do but

$('.active_page');

itself should give you the <a> element

Upvotes: 0

Explosion Pills
Explosion Pills

Reputation: 191809

You have to go up one level since it's the <li> that are siblings:

if ($(".active_page").parent().next().children('.page_link').length) {

}

If that's a little verbose for you, you can use indices:

if ($(".page_link").eq($(".active_page").index("li .page_link") + 1)).length) {

}

Just to prove that these both work: http://jsfiddle.net/ExplosionPIlls/t5NDX/

Upvotes: 4

Related Questions