tags:

views:

151

answers:

2

I have a potentially infinitely nested tree of ol > li

given an li in the tree, i need to apply some function to all other lis below the given li, except within the current ol. As an example:

for example:

<ol>
    <li>
        Do not apply to me
        <ol>
            <li>Do not apply to me</li>
            <li>Do not apply to me</li>
        </ol> 
    </li>
    <li>
        Do not apply to me
        <ol>
            <li id='given'>I am the "given" li</li>  <------------- you are here
            <li>Do not apply to me</li>
            <li>
                Do not apply to me
                <ol>
                    <li>Do not apply to me</li>
                    <li>Do not apply to me</li>
                </ol> 
            </li>
        </ol> 
    </li>
    <li>
        Apply to me
    </li>
    <li>
        Apply to me
        <ol>
            <li>Apply to me</li>
            <li>
                Apply to me
                <ol>
                    <li>Apply to me</li>
                    <li>Apply to me</li>
                </ol> 
            </li>
        </ol> 
    </li>
</ol>

What's the most elegant way to achieve this?

Thanks

+1  A: 

You can write:

   $("#given").parents("li:first").nextAll("li").find("li").andSelf();
Gordian Yuan
+1  A: 

@Gordon's answer will work with the given example, but if there is another OL immediately after the OL which contains #given, it won't work.

$("#given").parents("ol").nextAll("ol").find("li").each(function() {
    // function
});
$("#given").parents("li").nextAll("li").find("li").andSelf().each(function() {
    //function
});

This will work, but there's probably a more elegant way to accomplish it.

Chris B