-1

I have a site with a number of links. I want to always ensure the user knows what page he or she is by changing the color of the active link to red and a few more css properties. So i came up with the following

$('ul.nav li a').click(function()
{
    $(this).css('color','red');
});

So it works... kinds, only until the webpage redirects to the clicked link. Question is, how do i maintain the changes made using jquery after the page loads.

Thanks

John Kariuki
  • 4,966
  • 5
  • 21
  • 30
  • You necessary save options.. for instance using [cookie][1]. [1]: http://stackoverflow.com/questions/1458724/how-to-set-unset-cookie-with-jquery – Luca Davanzo Jul 07 '14 at 14:07

2 Answers2

0

First of all I clearly wouldn't do this using only javascript. But if you can't change anything serverside, you could store the

href

of the link you just clicked in the localStorage and read it on page load to initialize the color on the right link.

Here is a jsFiddle to illustrate : http://jsfiddle.net/5VLcL/

Pierre Fraisse
  • 970
  • 1
  • 11
  • 21
0

If you want to do this client side (using jQuery), you could check that the href is in the URL:

if (pathname.toLowerCase().indexOf(thisHref) >= 0)

Here's a fiddle for you - http://jsfiddle.net/ajthomascouk/phjCn/

Alex
  • 8,875
  • 2
  • 27
  • 44