function openSubMenu() { $('.work-examples__li').each(function () { testingByNik(); }); function testingByNik() { $(this).on("click", function () { //var tarGet = $(".work-examples .example.view-project-first"); //var activeBlock = $(".first .work-examples__li_active"); var tarGet = $(this).find(".example"); var activeBlock = $(this).find(".work-examples__li_active"); $(".projects__content .work-examples .example:not('.view-project-first')").removeClass("pop-up"); $(".projects__content .work-examples .work-examples__li_active:not('.first')").removeClass("visible"); if (!tarGet.hasClass('pop-up') && !activeBlock.hasClass('visible')) { tarGet.addClass('pop-up'); activeBlock.addClass('visible'); } else { tarGet.removeClass('pop-up'); activeBlock.removeClass('visible'); } }); } } And so, for each .work-examples__li I perform the function testingByNik This function is executed when I click on .work-examples__li . Next tarGet is .work_examples__li .example , a .activeBlock is .work-examples__li .work-examples__li_active. Right?
testingByNiknottestingByNikfor each.work-examples__li, but just so many times. Because you do not associate the function call and the elements found. - Alexey Ten$('.work-examples__li').each(testingByNik)- Grundy