Reverse tree scan, is it posible?
<p>I have an HTML structure like the following:</p>

<pre><code>&lt;ul id="mainMenu"&gt;
    &lt;li class="itm_1"&gt;
        Text...
    &lt;/li&gt;
    &lt;li class="itm_2"&gt;
        Text...
        &lt;ul&gt;
            &lt;li class="itm_3"&gt;
                Text...
                &lt;ul&gt;
                    &lt;li class="itm_4"&gt;
                        Text...
                    &lt;/li&gt;
                    &lt;li class="itm_5"&gt;
                        Text...
                    &lt;/li&gt;
                &lt;/ul&gt;
            &lt;/li&gt;
            &lt;li class="itm_6"&gt;
                Text...
            &lt;/li&gt;
        &lt;/ul&gt;
    &lt;/li&gt;
&lt;/ul&gt;
</code></pre>

<p>What I like to do, is to add a class to the active element and then in any other element in the same path to add another class like the example bellow:</p>

<pre><code>&lt;ul id="mainMenu"&gt;
    &lt;li class="itm_1"&gt;
        Text...
    &lt;/li&gt;
    &lt;li class="itm_2 ANCESTOR_ACTIVE"&gt;
        Text...
        &lt;ul&gt;
            &lt;li class="itm_3 ANCESTOR_ACTIVE"&gt;
                Text...
                &lt;ul&gt;
                    &lt;li class="itm_4"&gt;
                        Text...
                    &lt;/li&gt;
                    &lt;li class="itm_5 ACTIVE"&gt;
                        Text...
                    &lt;/li&gt;
                &lt;/ul&gt;
            &lt;/li&gt;
            &lt;li class="itm_6"&gt;
                Text...
            &lt;/li&gt;
        &lt;/ul&gt;
    &lt;/li&gt;
&lt;/ul&gt;
</code></pre>

<p>The element that will be considered as active I can find it and add the class of active, but I cannot find the way to add the ancestor_active class in the elements above the active element in the same path.</p>

<p>For now in my script I have the following code:</p>

<pre><code>&lt;?php
    $current_page = $page_id;    // Lets say is 5
?&gt;
&lt;script type="text/javascript"&gt;
    var cp = {
        p : &lt;?php echo $current_page; ?&gt;
    };

    jQuery(document).ready(
        function($)
        {
            $('.itm_' + cp.p).addClass('active');
        }
    );
&lt;/script&gt;
</code></pre>

<p><strong>Note</strong> : The nested arrays are not always with the same depth. In some cases there are other more nested lists and in some othere cases there are not sub lists
how can I climbe to the top element #mainMenu and add the classes "ancestor_active" to each li element in the same path using jQuery?</p>
