There are 2 lists.

The first:

<ul> <li class="lek" id="867" class="lek"> <span class="867">text1</span> </li> <li class="lek" id="865" class="lek"> <span class="865">text2</span> </li> <li class="lek" id="863" class="lek"> <span class="863">text3</span> </li> </ul> 

Second:

 <ul> <li> <a class="859 866 870" href="detail.php?id=933">aaa</a> </li> <li> <a class="863" href="detail.php?id=932">bbb</a> </li> <li> <a class="859 860 863 865 870" href="detail.php?id=931">ccc</a> </li> </ul> 

It is necessary that when you click, for example, on "text3", all elements with the class "863" are added to the class "select".

    2 answers 2

     $('.lek > span').click(function(){ var parentId = $(this).parent().attr('id'); // var className = $(this).attr('class'); $('.' + parentId).addClass('select'); }); 
    • The class is added to the current element, and it is necessary that all elements on the page. - Saturn
    • those. even non-existent at the moment on the page? Comrade @ karmadro4 once offered a way to dynamically add styles to css, here you can try - Specter
    • No, only existing. In your variant, the class "select" is added to the element it clicked on, i.e. to the "span" element. - Saturn
    • > In your version, the class "select" is added to the element that is clicked not if you want it to be added only to the links, for example, change the selector to $('a.' + parentId) - Specter
    • Strange, for some reason it does not work for me - Saturn
    1. .on ()
    2. .addClass ()