Sam
Sam

Reputation: 146

on click add class to aspx linkbutton with jquery

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

Upvotes: 0

Views: 1514

Answers (2)

Bongs
Bongs

Reputation: 5592

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

$(function() {
    $('a').live('click', function() {
        $(this).toggleClass('selected');
    });
});

Upvotes: 0

Jamie Dixon
Jamie Dixon

Reputation: 54011

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 */
}

Upvotes: 1

Related Questions