I have a menu, and in it when I click on a certain block, it becomes active, but when I click on the next one, it should stop being active. I wrote everything as in the example, but for some reason they continue to stand in active states.

$(document).ready(function(){ $('div[id^=block]').hide(); $('li.view').click(function(){ $('ul.Mynav>li:not(li.view)').**removeClass**('active'); $(this).**addClass**('active'); $('div#block1').fadeIn(200); $('div[id^=block]:not(div#block1)').fadeOut(200); }); $('li.company').click(function(){ $('ul.Mynav>li:not(li.company)').removeClass('active'); $(this).addClass('active'); $('div#block2').fadeIn(200); $('div[id^=block]:not(div#block2)').fadeOut(200); }); }); 
 .active{ background-color:#3399ff; } 

I rechecked several times ... (in the video for which the site did, it is said that add adds and disables (something like), but does not work. https://jsfiddle.net/acac2c7n/ Here.

Closed due to the fact that the participants are off topic: Athari , aleksandr barakin , Vladyslav Matviienko , dizballanze , Alexey Shtanko 12 Jun '15 at 5:57 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • "Questions asking for help with debugging (" why does this code not work? ") Should include the desired behavior , a specific problem or error, and the minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, complete, repeatable example . " - Athari, aleksandr barakin, Vladyslav Matviienko, dizballanze, Alexey Shtanko
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • Step number 1. Provide a self-contained code so that it is clear what is going on (where is HTML? What is the allocation of asterisks in the code?). Step number 2. Throw out a video if it teaches you to write such code. Creepy noodles. - Athari

1 answer 1

Yes, asterisks here .**removeClass**( and here .**addClass**( definitely superfluous.
$('ul.Mynav>li:not(li.company)').removeClass('active'); - removes the active class from all items of your menu except those of the company class.
Those. if there are several, they will remain in the active state when switching between them.
Here is a working example.

  • Thanks, I just wrote too much looking at that example. - Laka Biglife