When you click on a particular .window, it is assigned a class .opened with the standard click-addClass. How can I remove the .opened class from a specific window by clicking on "close"?

$(document).ready(function() { $(".cursors").click(function() { $(this).addClass('currentz'); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="col-md-4 mainnew2 cursors "> <div class="relaliv"> <figure class="full-image-ratio ratio-1-1 zoomIn wow" data-wow-duration="0.3s" data-wow-delay="0.3s"> <img src="<?php the_sub_field('i'); ?>"> </figure> <div class="full-image-caption"> <h4> <?php the_sub_field('n'); ?> </h4> </div> </div> <div class="usloviyayay"> <div class="vertimidle"> <h4 class="h4t"> <?php the_sub_field('n'); ?> </h4> <p class="magi"> <?php the_sub_field('v'); ?> </p> <div class="button-alignment"> <a class="button-outline has-down-state losess"> <span class="button-outline-copy small-padding"> <span class=""> <span class="button-text-wrapper">закрыть</span> </span> </span> <span class="button-outline-bottom"></span> </a> </div> </div> </div> </div> 

  • add chtoli your css classes, and attempts to implement what you need - Rostyslav Kuzmovych
  • Besides, it is not clear. You need an answer to pure js or using jquery. It is also not clear if you have simplified the nesting structure of your DOM for the question. Or is it originally yours? - GONG
  • @GONG The answer is possible with jquery. Slightly edited the code. The point is that there are a lot of blocks on which content is displayed by clicking (within a few more divs) + the close button. - Mimzik

1 answer 1

You can use the .closest function, which finds the nearest specified element in the DOM

 $('.close').click(function() { var nearestWindow = $(this).closest('.window'); //возможно проверка в вашем случае будет лишней. Т.к. кнопка находится непосредственно внутри элемента с нужным классом. if(nearestWindow !== null && nearestWindow.hasClass('.opened')) nearestWindow.removeClass('.opened'); }); 
  • Laid out the full version of one of the many such windows. That's probably why your answer doesn't work - Mimzik
  • Thanks for the tip. Made according to your principle - Mimzik