[jQuery] Keeping track of the current clicked link
Hi, these are my first attempts with jQuery and I would be glad if
somebody could help me.
My Problem: I'm using the ASP.net MVC framework and wanted to keep
track of the current clicked menu link. The menu links are in the
master page and so is the jscript.
What I tried to do is to attach some code to the click event of my
links (in a marked list) and attach a "selected" class to the current
clicked link:
sample code 1>>>
i started with this little jquery script:
$(document).ready(function() {
$("#menu a").click(function() {
$(this).parents("li").addClass("active");
});
});
<<<end sample code 1
but soon found out that because the click function reloads the page it
does apply the css right after the click but looses the class info
after the (other content's ) page recreation.
I played around with storing the var outside the call but it always
led to the result that all info is lost because I actually navigate to
a new page.
Finally I tried the approach below, where I use the ready function for
the click event so that the page actually is done with the navigation
but again I'll loose the context of the this function - although it
should keep it when I read the documentation of chaining jQuery.... I
already tried to return the element with this line "return $
(this).parents.("li") inside of the click function but that does not
work either. Can somebody help me please? Or is it not possible
because of the actual navigation?
sample code 2>>>
$(document).ready(function() {
$("#menu a").click(function() {
}).ready(function() {
$(this).parents("li").addClass("active");
});
});
<<<end sample code 2