bombai
bombai

Reputation: 967

Apply add/remove class to specific tag Jquery

I have one menu list like this..

            </div>
            <ul id="menu">
                <li id="About">@Html.ActionLink(@MvcWebRole1.Resources.Shared.Layout.About, "About", "Home")</li>
                <li id="Services">@Html.ActionLink(@MvcWebRole1.Resources.Shared.Layout.Services, "Services", "Home")</li>
                <li id="Partners">@Html.ActionLink(@MvcWebRole1.Resources.Shared.Layout.Partners, "Partners", "Home")</li>
.....
.....

And i want to add/remove a class to a specific tag when i click on a button with a jquery function..

How can i apply this one to a specific menu>li>id="About"??

I have tried something like this but it doesn't work..

$('#menu li About').removeClass('classname'); 

and

$('#menu li').attr(About).removeClass('classname'); 

and

if ($('#menu li').attr("id") == "About")
  $('#menu li').removeClass('classname');

Thanks.

Upvotes: 2

Views: 1866

Answers (4)

Lukx
Lukx

Reputation: 1283

please make yourself familiar with the way jQuery Selectors work ( http://api.jquery.com/category/selectors/ ).

What you are looking for is the ID selector, which you do use on the #menu-Part, thus $('#About') would be the correct selector.

Upvotes: 1

David
David

Reputation: 218808

Close. This code:

$('#menu li About')

Is looking for an element of id menu which contains an element named li which contains an element named About. You need to specify that it's another id (and is an id on the li):

$('#menu li#About')

Of course, since ids are supposed to be unique within the document, this should work just as well:

$('#About')

Upvotes: 2

thecodeparadox
thecodeparadox

Reputation: 87073

$('#menu li#About').removeClass('classname'); 

or just

$('li#About').removeClass('classname');

or

$('#About').removeClass('classname');

or

$('#About', '#menu').removeClass('classname');

Upvotes: 2

Sagiv Ofek
Sagiv Ofek

Reputation: 25280

your selector is wrong. select the id you need. try:

('#About').removeClass('classname'); 

Upvotes: 3

Related Questions