sunchild
sunchild

Reputation: 49

Jquery removeClass nothing work

Sample code and example is here.

$("#menus > li > ul > li > a").click(function(){
    $(this).toggleClass("selected").siblings().removeClass("selected");

but click and selected background is work an other click multi selection is not remove not work... Please help and solutions...

Upvotes: 2

Views: 138

Answers (3)

Arun P Johny
Arun P Johny

Reputation: 388316

You have the click handlers registered to a elements, but it has no siblings. You need to go to the parent li element find its siblings then drill down to the a elements inside those sibling elements

You need

$("#menus > li > ul > li > a").click(function(){
    $(this).toggleClass("selected").parent().siblings().find('a').removeClass("selected");
});

Demo: Fiddle

It again has a bug, if you navigate between two types, so try

$("#menus > li > ul > li > a").click(function(){
    $('#menus a.selected').removeClass("selected");
    $(this).toggleClass("selected");
});

Demo: Fiddle

Upvotes: 1

Sonu Sindhu
Sonu Sindhu

Reputation: 1792

try this

 $("#menus > li > ul > li > a").click(function(){
        $("#menus > li > ul > li > a").removeClass('selected');
        $(this).toggleClass("selected");

});

Hope it will help

Upvotes: 1

Armel Larcier
Armel Larcier

Reputation: 16027

Your a element has no siblings. You have to select a elements in the parent li's siblings.

$("#menus > li > ul > li > a").click(function(){
    $('a',$(this).toggleClass("selected").parent().siblings()).removeClass("selected");
}

See it working: http://jsfiddle.net/QFWLk/1/

Upvotes: 1

Related Questions