tags:

views:

43

answers:

4

i am trying to use the plugin infinite scroll

it requires the selector for the next page link. i suppose that if the navigation is as follows,

<ul>
    <li><a href="#" class="active">page 1</a></li>
    <li><a href="#" class="next">page 2</a></li>
    <li><a href="#">page 3</a></li>
    <li><a href="#">page 4</a></li>
    <li><a href="#">page 5</a></li>
</ul>

i can use "a.next" as the selector for the next page. but what happens if my page nav markup is as follows, without the .next class

<ul>
    <li><a href="#" class="active">page 1</a></li>
    <li><a href="#">page 2</a></li>
    <li><a href="#">page 3</a></li>
    <li><a href="#">page 4</a></li>
    <li><a href="#">page 5</a></li>
</ul>

how can i select the page 2 link assuming .active signifies the current page

+2  A: 

To select the node next to .active, you can use the next() function:

$('.active').next();

But why not add a prev/next list item that's always there?

Peter Kruithof
the next() method "Get the immediately following sibling of each element in the set of matched elements, optionally filtered by a selector." There are no immediate siblings of the active class
John Hartsock
Yes it. It looks at the element and not at the class
Trefex
It's the right idea, though. Perhaps something like $('.active').parent().next().children().first() (Yeah, kind of long and complicated, but it works).
Ryan Kinal
@John Hartsock The immediate siblings of the active class are the other <li> nodes. My example works, I've put together a snippet to demonstrate: http://jsfiddle.net/QdV7q/ (don't be surprised by the alerts ;) )
Peter Kruithof
@Peter - Your jsFiddle changes the OP's HTML. Take a closer look. The `.active` class is on an `<a>` element *inside* the `<li>`. Yours placed `.active` directly on the `<li>`.
patrick dw
Right, I overlooked that. Seems like Ryan got it right the first time!
Peter Kruithof
oh ya, i forgot abt the prev/next link
jiewmeng
Good job everyone it seem like you guys worked it out
John Hartsock
A: 

Hi,

I think you have to add a unique id to your <a> tags in order to be able to use the plugin.

Cheers,

Trefex
+1  A: 

Try

$('.active').parent().next().children().eq(0);
Kai
+2  A: 

$("li:has(.active) + li a") will do the job. E.g.

alert($("li:has(.active) + li a").text());
Gert G
+1 for better solution!
Kai
i happen to feel that since i am able to use **prev/next** links it is straight forward but i agree that if i am constraint to having `.active` class this maybe better ... hmm but will that selector select **all** siblings rather than the **single next** link?
jiewmeng
This solution works on the example you had in the OP. If you were to have more than one anchor in the `LI` and needed to target the first anchor in the next `LI`, then you would have to add `:first` to the statement. E.g. `$("li:has(.active) + li a:first").text();`
Gert G