I need to create a menu with 3 buttons that when clicked, it opens a div on the side of the menu. The 3 buttons open the same div. I was able to create this without a problem. The actual problem I have is when I want to hide the div. I only want to hide the div when you click on a button that has .open
as a class, so I can't use .toggle()
... Am I even doing this right?
Here's a JSFiddle: https://jsfiddle.net/5duh8v71/3/
$('.btn').click(function() {
var obj = $(this);
if ($('.btn').hasClass('open')) {
$('.btn').removeClass('open');
}
if (obj.hasClass('open')) {
obj.removeClass('open');
} else {
obj.addClass('open');
}
$(".hidden-content").show("slide");
});
//This is what I tought would solve my problem...
$('.btn.open').click(function() {
var obj = $(this);
obj.removeClass('open');
$(".hidden-content").hide("slide");
});
.container {
display: flex;
}
.menu {
width: 50%;
}
.btn {
background-color: #1f2227;
padding: 10px;
margin: 5px;
border-radius: 5px;
color: #ffffff;
cursor: pointer;
&:hover {
background-color: #0f1113;
}
&.open {
background-color: #0f1113;
}
}
.hidden-content {
padding: 10px;
border: 1px solid #0f1113;
border-radius: 5px;
width: 50%;
margin: 5px;
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
<div class="menu">
<div class="btn">CLICK</div>
<div class="btn">CLICK</div>
<div class="btn">CLICK</div>
</div>
<div class="hidden-content">
<h1>Content</h1>
</div>
</div>