If one menu-item is clicked then the description with the same data-filter attribute should be shown.
I added a class to the description elements to associate each one with the affiliated menu item.
What do you think?
$(".button").click(function() {
$(".button").removeClass("active-filter");
$(this).addClass("active-filter");
var button = $(this).attr("data-filter");
var active_element = $('.descr[data-filter="' + button + '"]');
$(".descr").removeClass("active-element");
active_element.addClass("active-element");
});
.element {
display: inline-block;
background-color: lightgreen;
width: 220px;
}
.filter {
display: inline-block;
margin: 10px;
background-color: orange;
}
.button {
width: 60px;
height: 60px;
padding: 10px;
margin: 10px;
background-color: lightgrey;
float: left;
}
.descr {
display: block;
max-height: 0;
visibility: hidden;
background-color: lightgrey;
padding: 10px;
position: absolute;
top: 0;
left: 0;
right: 0;
margin: 10px;
}
.data-element {
height: 60px;
display: block;
background-color: orange;
margin: 10px;
position: relative;
}
.active-filter {
background-color: yellow;
display: block;
}
.active-element {
background-color: yellow;
max-height: 100%;
visibility: visible;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="element">
<div class="filter">
<div class="button active-filter" data-filter="3">data-filter 1</div>
<div class="button" data-filter="4">data-filter 2</div>
</div>
<div class="data-element">
<div class="descr active-element" data-filter="3">data-element 1</div>
<div class="descr" data-filter="4">data-element 2</div>
</div>
</div>
var active_element = $('.d[data-filter="' + b + '"]');
\$\endgroup\$.find()
or.filter()
to chain jQuery methods, instead of declaring multiple variables. \$\endgroup\$