Cofey
Cofey

Reputation: 11414

How to find out the previously selected navigation link in jQuery?

I have a navigation set up using ULs, LIs, and As. The hightlighted/selected tab's anchor has a class of selected. When a new nav link is clicked, how can I find out what the ID of the previously selected anchor's tab is?

Upvotes: 1

Views: 176

Answers (2)

Mouhannad
Mouhannad

Reputation: 2209

try this:

html

<ul>
    <li><a href="#">link1</a></li>
    <li><a href="#">link2</a></li>
</ul>

js

$("a").click(function() {
   var $current = $(this);
   var $prev = $current.closest("ul").find("a.selected");
    if($prev.length) {
       alert($prev.text())   
       $prev.removeClass("selected");
    }
    $current.addClass("selected");
    return false;
});

Upvotes: 1

Diodeus - James MacFarlane
Diodeus - James MacFarlane

Reputation: 114447

myId = $('a.selected').attr('id')

Upvotes: 0

Related Questions