Within a container, I have several div elements where .div-to-hide
is shown by default and .div-to-show
is hidden.
My goal is to toggle the visibility of these elements - when clicking on a .set, .div-to-hide
should hide and .div-to-show
should become visible. Subsequent clicks should revert the previously clicked element to its original state.
Additionally, I want to display two buttons inside each .div-to-show
.
<div class="container">
<div class="set">
<div class="div-to-hide">Some text</div>
<div class="div-to-show"></div>
</div>
<div class="set">
<div class="div-to-hide">Some text</div>
<div class="div-to-show"></div>
</div>
<div class="set">
<div class="div-to-hide">Some text</div>
<div class="div-to-show"></div>
</div>
</div>
This is my current code snippet:
let lastClicked;
$('.container').on('click', function(e) {
if (this == lastClicked) {
lastClicked = '';
$('.div-to-hide').show();
$(this).children('.div-to-hide').hide();
} else {
lastClicked = this;
$('.div-to-hide').hide();
$(this).children('.div-to-hide').show();
$(this).children('.div-to-show').hide();
}
});
I'm facing some issues with making it work as intended. Any assistance would be greatly appreciated!
UPDATE: I managed to fix it! Thanks for all the help!