tags:

views:

33

answers:

1

Hi there

I have a main menu:

<ul class="menu">
<li><a href="/"><span class="l"></span><span class="r"></span><span class="t">Home</span></a></li>
<li><a href="<%= Url.Action("Login", "User")%>"><span class="l"></span><span class="r"></span><span class="t">My Account</span></a></li>
<li><a href="#"><span class="l"></span><span class="r"></span><span class="t">Post Idea</span></a></li>
<li><a href="#"><span class="l"></span><span class="r"></span><span class="t">Browse Ideas</span></a></li>
<li><a href="#"><span class="l"></span><span class="r"></span><span class="t">Invest in Ideas</span></a></li>

And i am trying to select the active link using jQuery:

<script type="text/javascript">
    $(document).ready(function () {
        var path = location.pathname;
        var home = "/";
        $("a[href='" + [path || home] + "']").addClass("top-menuactive");
    });
</script>

However the javascript depicted above finds all links on site, not just the links in the menu.

How can i change the script to select only the links within<ul class="menu">

+2  A: 

You can use the descendant selector:

<script type="text/javascript">
    $(document).ready(function () {
        var path = location.pathname;
        var home = "/";
        $("ul.menu a[href='" + [path || home] + "']").addClass("top-menuactive");
    });
</script>
MvanGeest
thanx... just figured it out...i used $(".menu a[href='" + [path || home] + "']").addClass("top-menuactive");is the ul.menu important?
Dusty Roberts
Not really, unless you have other elements with the "menu" class. I'm not sure if `ul.menu` is faster, as I don't know how jQuery implements `$()`.
MvanGeest
kewl... thanx for the help :)
Dusty Roberts