How can i take the previousElementSibling of all previousElementSibling of one element using JS

20 Views Asked by At

I trie to get every the previouselementSibling of a div, but stil have problems with that. There is a way to get this?


garrafinhas.forEach((garrafa,indice)=>{
    garrafa.addEventListener('click',()=>{
        if(garrafa.classList.contains('garrafinha') ){
            garrafa.classList.remove('garrafinha')
        }
        else
        {
            while(garrafa.previousElementSibling !== 0){
                garrafa = garrafa.previousElementSibling;
                garrafa.classList.add('garrafinha')
                garrafa.classList.add('garrafinha')
            }
        }
        console.log(indice, garrafinhas.length)
    })
})

0

There are 0 best solutions below