tags:

views:

119

answers:

3

This might be tricky.

Php will generate:

<li>element</li>
<li>element</li>
<li class="alt">element</li>
<li class="alt">element</li>
<li class="alt">element</li>
<li>element</li>
<li>element</li>
<li class="alt">element</li>
<li class="alt">element</li>
<li class="alt">element</li>
<li>element</li>

What I need is a way to find the li elements with the .alt class until the li elements start. So...

<li>element</li>
<li>element</li> // if I click on this one, it will select
<li class="alt">element</li> // this one
<li class="alt">element</li> // and this one
<li class="alt">element</li> // and this one
<li>element</li> // stops selecting here
<li>element</li>
<li class="alt">element</li> // doe not this one
<li class="alt">element</li> // or this one
<li class="alt">element</li> // or any other .alt
<li>element</li>

I tried doing this with .find(), but I can't find a way to divide the .alt elements into groups and isolate the first one.

Any braniacs there with a good idea?

A: 

try (couple of edits but think I have it)

$('li:contains(element)').click().find('li:contains(element):not[alt]:first').prevAll('li.alt');

Mark Schultheiss
A: 

couldn't you add an extra class for the lis thats gonna be retrieved?

uji
+4  A: 

If you're using jquery 1.4 you can use the new nextUntil method.

http://api.jquery.com/nextUntil/

edit - example:

$("li").click(function() {

   $(this).nextUntil("li[class='']").each(function() {           
    console.log($(this));           
   });

});
ScottE
I just read about this the other day. Me and my memory, thank you very much mate.
daulex
+1 for getting 1.4 adoption push
Mark Schultheiss
@daulex - no problem@mark - yes, this method is quite a handy one. I can think of many cases where I could have used it before. I also enjoy the more intuitive .index() usage.
ScottE