What I have is
<div>
<div onclick="find(this)" class="grey">1</div>
<div onclick="find(this)" class="grey">2</div>
<div onclick="find(this)" class="grey">3</div>
<div onclick="find(this)" class="grey">4</div>
<div onclick="find(this)" class="grey">5</div>
</div>
What I like to do is to click on a <div>
(for example:<div>...3</div>
)
and replace the class for div 3 and all it's previous sibling 'red' and all next sibling 'grey'.
What I thought would work is to loop backwards till it reaches the parent element.
What I've been trying.
function find(ele) {
var active = ele;
var previous = active.previousElementSibling;
var next = active.nextElementSibling;
var list = []; // Trying to find a way to push all previous siblings of active into this array
var i = 0;
while (i < list){
next.classList.replace('grey')
previous.classList.replace('red');
i++
}
}
With insufficient knowledge to complete my function there is a few undefined that I need help with, but after thinking through, this function might not work because everytime the function is called previous sibling will return to grey.
So it there a way to make this work?
Like only elements in the list array replace class to red and those not in the array replace to grey.
Thank you for your help, really appreciate whoever you are for the help.