I'm trying to do in vanilla js what the siblings() does in jQuery. I've seen a lot of helpful explanations here but I'm having trouble implementing them on my code.
I have a div that has 4 buttons inside it. Every time i click on a button a class (.btn-anim) is added to that button (this works ok so far). What I want to do is when I click on one of the buttons (that doesn't have the class already) to remove the class of any other button and add it to the clicked one.
My Html markup:
<div id="js-colors-div">
<button class="yellow"></button>
<button class="green"></button>
<button class="blue"></button>
<button class="pink"></button>
</div>
And Js:
var colorsDiv = document.getElementById('js-colors-div');
var colors = colorsDiv.getElementsByTagName("button");
for (var i = 0; i < colors.length; i++) {
colors[i].onclick = function(e) {
var highlight = e.target;
//trying to achieve this
$(this).addClass('btn-anim').siblings().removeClass('btn-anim');
}
};
And this is my code for adding the class
highlight.classList.add('btn-anim'); //