var toggleButton = document.getElementsByClassName('toggle-button')
for (var i = 0; i < toggleButton.length; i++) {
toggleButton[i].addEventListener('click', function () {
toggleButton[i].parentElement.parentElement.children[2].style.background = "red";
});
}
<div>
<div>
<button class="toggle-button">view</button>
</div>
<div>content</div>
<div>the div which I want to apply changes on</div>
</div>
What's wrong with this code? When I run it it gives me this error "Uncaught TypeError: Cannot read property 'parentElement' of undefined at HTMLButtonElement."
*I don't want to apply the CSS changes on a class I need it to be applied on this specific div using DOM.