I've created a fiddle using the same html structure of a plugin I'm trying to edit. There are two 'grandparent' elements containing their ancestors, and I'm trying to use a button contained in one of them to toggle a class on an overlay contained in the other.
I've been trying various jQuery combinations, but so far nothing has worked. This is the one that I feel is most logical, can anyone point out where I'm going wrong?
It might be worth me mentioning that I don't have much flexibility in editing the html structure of the plugin, just the CSS and jQuery.
https://jsfiddle.net/71qdzr9j/4/
jQuery(document).ready(function($) {
$(".button").click(function() {
$(this).parentsUntil(".lower-section").previous().find(".overlay").toggleClass("open");
});
});
.wrapper {
position:relative;
top:0;
float:left;
display:inline-block;
width:275px;
background:pink;
margin-right:24px;
}
.overlay {
position: absolute !important;
width: 100% !important;
height: 100% !important;
top: 0 !important;
left: 0 !important;
background-color: rgba(0,0,0,0.5) !important;
opacity: 0 !important;
}
.overlay.open {
opacity:1;
}
.lower-section {
position:relative;
}
.button {
position:absolute;
display:block;
top:0;
right:0;
background:orange;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<div class="wrapper">
<a class="upper-section" href="">
<div class="parent">
<img class="child" width="275px" height="275px" src="https://i.ibb.co/vjsFfPq/Screenshot-2020-10-03-at-17-30-33.png"/>
<span class="overlay"></span>
</div>
</a>
<div class="lower-section">
<div class="lower-section-inner">
<div class="title">Autumn Trees</div>
<div class="description">Mist rolling in over the horizion.</div>
<span class="button">Button</span>
</div>
</div>
</div>
<div class="wrapper">
<a class="upper-section" href="">
<div class="parent">
<img class="child" width="275px" height="275px" src="https://i.ibb.co/020Lc1f/Screenshot-2020-10-03-at-17-31-09.png"/>
<span class="overlay"></span>
</div>
</a>
<div class="lower-section">
<div class="lower-section-inner">
<div class="title">Ice patterns</div>
<div class="description">Detail of a frozen landscape.</div>
<span class="button">Button</span>
</div>
</div>
</div>