0

I have many link buttons with various contents like tabs, i need to apply a class to clicked link button with jquery, am using bellow function but its not working when post backing the page

$(function() {
    $('a').click(function() {
        $(this).addClass('selected');
    }, function() {
        $(this).removeClass('selected');
    });
});

Thanks in advance

Sam
  • 146
  • 15

2 Answers2

1

This won't work they way you're expecting it to becuase the class is added and then the page is refreshed.

You don't really need any Javascript for this, here's how I do it.

Each page has it's own ID

<body id="aboutPage">

and each menu item has its own ID too:

<li id="aboutMenuItem">About</li>

Then in your CSS you can specifically hit the li for the page you're on:

#home #homeMenuItem,
#about #aboutMenuItem,
[...]
{
 /* Style for the selected menu item here */
}
Jamie Dixon
  • 53,019
  • 19
  • 125
  • 162
0

Try to bind the click event or do something like this...

$(function() {
    $('a').live('click', function() {
        $(this).toggleClass('selected');
    });
});
Bongs
  • 5,502
  • 4
  • 30
  • 50
  • This won't work either. The live binding will simply perform the same as the click in this case and once the link is clicked, the class will be toggled and then the page refreshed - resetting everything back to its default. – Jamie Dixon Apr 23 '12 at 07:17