I am looping over a group of elements with the same class. Now when an element is clicked I need it to get a class that makes it active and adds some css to it. This works but it stays forever, so if I click 4 elements I have 4 active elements.
I'm trying to remove the class from all sibling elements when 1 is clicked but below code does not work for some reason:
$('.klantenblokje').each(function(i, obj) {
$(this).on("click", function(e){
e.preventDefault();
var id = $(this).attr('id');
$(this).addClass('activeblok');
$(this).siblings().removeClass('activeblok');
});
});
I've also tried:
$('.klantenblokje').each(function(i, obj) {
$(this).on("click", function(e){
e.preventDefault();
var id = $(this).attr('id');
$(this).addClass('activeblok').siblings().removeClass('activeblok');
});
});
Why doesn't it work? The class is added when I click, but for the siblings nothing is removed.
My HTML if that helps:
<div class="col-md-2 col-6">
<a href="#bezorgen" id="bezorgen" class="klantenblokje">
<i class="icon-transport"></i>
<span>Bezorgen</span>
</a>
</div>
<div class="col-md-2 col-6">
<a href="#bestellen" id="bestellen" class="klantenblokje">
<i class="icon-cart"></i>
<span>Bestellen</span>
</a>
</div>
<div class="col-md-2 col-6">
<a href="#betalen" id="betalen" class="klantenblokje">
<i class="icon-euro"></i>
<span>Betalen</span>
</a>
</div>
<div class="col-md-2 col-6">
<a href="#uploaden" id="uploaden" class="klantenblokje">
<i class="icon-upload"></i>
<span>Bestanden uploaden</span>
</a>
</div>
<div class="col-md-2 col-6">
<a href="#garantie-reparatie" id="garantie-reparatie" class="klantenblokje">
<i class="icon-box"></i>
<span>Garantie en reparatie</span>
</a>
</div>
<div class="col-md-2 col-6">
<a href="#account" id="account" class="klantenblokje">
<i class="icon-user"></i>
<span>Account</span>
</a>
</div>