I'm trying to find out how to edit this jQuery code so that only the parent whose button is clicked shows its child. At the moment, all the children are shown when any button is clicked, not the one nested in the targeted parent.
I have tried using
$(this).find(".child").toggleClass("open");
but this didn't work.
Any help much appreciated.
https://jsfiddle.net/jf1ya0cz/2/
HTML
<div class="parent">
<div class="child">
</div>
</div>
<div class="parent">
<div class="child">
</div>
</div>
<div class="parent">
<div class="child">
</div>
</div>
CSS
.parent {
position:relative;
width:100px;
height:100px;
background:red;
display:inline-block;
float:left;
margin-right:6px;
}
.child {
width:inherit;
height:30px;
position:absolute;
bottom:0;
background-color:blue;
opacity:0;
transition:0.5s;
}
.child.open {
opacity:1;
}
.button {
float: left;
background: yellow;
width: 40px;
height: 40px;
position: absolute;
top: 0;
left: 0;
}
jQuery
jQuery(document).ready(function($) {
$( "<span class='button'></span>" ).insertBefore( ".child" );
$(".button").click(function() {
$(".child").toggleClass("open");
});
});