enter code here
classesFilterWerke = [
'skulptur',
'malerei',
'druck',
'neueMedien',
'sozialkritischs',
'performance',
'installation',
'kunstImÖffentlichenRaum'
];
for( var i = 0; i < classesFilterWerke.length; i++ ) {
iNthChild = i+2
$('.werke-filter button:nth-child('+ iNthChild +')').click(function(){
$('.'+ classesFilterWerke[i] +'').toggle();
})
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="werke-filter filter-wrapper">
<p class="float-left">Filter:</p>
<button class="active btn btn-primary-outline">Skulptur</button>
<button class="active btn btn-primary-outline">Malerei</button>
<button class="btn btn-primary-outline">Druck</button>
<button class="btn btn-primary-outline">Neue Medien</button>
<button class="btn btn-primary-outline">Sozialkritisches</button>
<button class="btn btn-primary-outline">Performance</button>
<button class="btn btn-primary-outline">Installation</button>
<button class="btn btn-primary-outline">Kunst im öffentlichen Raum</button>
</div>
<a href="werk-baldachin.php?id=8" class="karte skulptur" data-aos="fade-up">
<div class="karte-img">
<img src="../assets/img/werke/thumb-skulptur-longboard.jpg" alt="">
</div>
<div class="stroke-bottom"></div>
<h4>Longboard</h4>
<h5>Skulptur</h5>
</a>
<a href="werk-baldachin.php" class="karte druck" data-aos="fade-up">
<div class="karte-img">
<img src="../assets/img/werke/thumb-skulptur-schale.jpg" alt="">
</div>
<div class="stroke-bottom"></div>
<h4>Keramikton Arbeiten</h4>
<h5>Skulptur</h5>
</a>
<a href="werk-baldachin.php" class="karte malerei" data-aos="fade-up">
<div class="karte-img">
<img src="../assets/img/werke/thumb-skulptur-stehleuchte.jpg" alt="">
</div>
<div class="stroke-bottom"></div>
<h4>Stehleuchte</h4>
<h5>Skulptur</h5>
</a>
I want to make a filter which toggles elements with a certain class. My problem now is that if I use the jQuery .click Event Listener inside a while loop it doesn't work. If I get the jQuery statement outside of the while loop it works.
NOTE: I haven't used the i variable and the classesFilterWerke (Array) inside the loop because it doesn't work even without.