views:

126

answers:

3

I have a load of LIs inside an UL, and each one has a unique ID.

Given two IDs, what's the best way to select the two corresponding LIs, and all the LIs in between?

Thanks!

+2  A: 

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

Kind Regards

--Andy

jAndy
+1  A: 

You can do it like this:

$('#id').nextUntil('#id2').andSelf().add('#id2')

It's important to note that .nextUntil() does not include the element it's going until, it stops with the one before it. To add that element, you need to call .add(selector) on the end.

You also need a .andSelf() to include the first element

Nick Craver
+ .addSelf() will do it.
jAndy
@jAndy - `.addSelf()` would include the original ID, not the ending element, you still need `.add()` for the ending ID
Nick Craver
@Nick: which is exactly what he wants.
jAndy
@jAndy if you have li 1, 2, 3, 4, without the `.add()`, 4 will NOT be included, `.nextUntil()` is non-inclusive. From the docs: "The new jQuery object that is returned contains all following siblings up to but not including the one matched by the `.nextUntil()` selector."
Nick Craver
I know, I didn't mean to append it, just to add it into the expression
jAndy
@jAndy - Ah, yes in that case we're on the same page, was testing in fiddle when I realized that as well.
Nick Craver
what me actually brings to the question, if there is a way in jQuery with that you can get the 'root' selector in a later function. That would replace the .add('#id2') into something like .add(':next') or .add(root.next()) mmmhhh!
jAndy
@jAndy - It's possible, resig did a post on this a while back about a chaining porposal: http://ejohn.org/blog/ultra-chaining-with-jquery/ If you combined with using the `$(...).selector` property you could use it in the chain, but it'd be pretty hard to follow if it got deep :)
Nick Craver
great stuff guys, thanks.
aidan
A: 

To select the All LIs in Between UL

 $(document).ready(function(){
            $("li[ID*=ids]").css("bacground-color","red");// contian common world ex ids1
    });
ricky roy
This would select all `<li>` with an `id` containing `ids`...this doesn't at all answer the question.
Nick Craver
Not my downvote, but based on other answers this morning I've seen...you need to read the questions a bit better, an off-topic or completely incorrect answer is rarely helpful
Nick Craver