I have a whole list of link elements. Each class is assigned. Clm and different id. And there are classes in CSS with exactly the same names as id, but by default they are turned off. It is necessary to make it so that when you click on the link, the name id is somehow pulled into a variable and for .back-image (the class where the background changes) an additional class of those hidden {addClass (this.id)} is added. include a class with the same name. Some code. CSS:

//по умолчанию они нигде не присвоены, назовем их "скрытые". .arts{ background: url("../images/arts.svg") no-repeat center center; } .video{ background: url("../images/video.svg") no-repeat center center; } 

(x) HTML:

 <a xlink:href="#" id="arts" class="clm">Arts</a> <a xlink:href="#" id="video" class="clm">Video</a> <div class="back_img"></div> //там меняется фон, который добавляется из какого-либо скрытого класса 

My cripple js:

 $(document).ready(function(){ $('.clm').клик(получить его id) }); $('.clm').click(function()){ $('.back_img').addClass(класс с именем как и у id) } }); 

    1 answer 1

    If I understood correctly, then so.

     $('.clm').click(function(){ var id = $(this).prop('id'); // можно ещё так this.id $('.back_img').addClass(id); }); 
    • Yes, almost) Only the first time I can turn it on. But the following buttons no longer work. After addClass (and before I tried) I added such $ ('. Back_img'). RemoveClass (id); but it doesn’t work - Artyom Oleynichenko
    • @ArtyomOleynichenko - so explain when removeClass should be removeClass - there is nothing to removeClass about this. - Igor
    • No, it worked. I knowingly deleted the first $ (document) .ready (function () - it didn't work without it. Thank you) - Artyom Oleynichenko
    • Only if you click often will two classes be added at once. You can add logic with .hasClass() or .toggleClass() or hardcorely do $('.back_img').removeClass('arts').removeClass('video') - Eugene Merkushev