views:

83

answers:

2

I have following markup

<div id="mnuMain">
    <ul>
        <li><a href="#">Dashboard</a></li>
        <li><a href="#">Market Trends</a></li>
        <li><a href="#">Master</a>
            <ul>
                <li><a href="#">Products</a></li>
                <li><a href="#">Segments</a></li>
                <li><a href="#" >Companies</a></li>
            </ul>
        </li>
        <li><a href="#">Settings</a>
            <ul>
                <li><a href="#">Dashboard Settings</a></li>
            </ul>
        </li>
    </ul>
    <br style="clear: left" />
</div>

<div id="divNavigation" style="height:20px;width:100%;
background:gray;color:white">        
</div> 

My question is

How to track parents upward when I click on particular <a> so that divNavigation will contain suppose now I have clicked on "Segments" divNavigation should have Master > Segments with links.

A: 

$("#mya").parent() for accessing the immediate parent. Use parents() for accessing all ancestors. This is if you have id. If you don't then you may need something like this:

$("a").each( var parent = $(this).parent(); <use parent to do stuff>);
Sidharth Panwar
+1  A: 

rushed codes

$(function() {
    $('#mnuMain ul li a').click(function() {

        var $li = $(this).parents('li');
        var container = $('#divNavigation').empty();

        $li.each(function(i) {
            if (i > 0) {
                $('<span>&gt;</span>').prependTo(container);
            }
            $(this).children('a:first').clone().prependTo(container);
        });

        return false;
    });
});​

demo

Reigel
Thank you very much....what if I click on link in that sitemap..will it get updated as per hierarchy?
cloudlight
I have done that....thanks once again...
cloudlight