I have two div
elements with one button in the first div as follows.
<div class='my-class'>
<div class='other-div'>
<button onClick="nextDiv(this);">Click</button>
</div>
</div>
<div class='my-class'>
</div>
Below is my JavaScript code.
function nextDiv(element) {
element.closest('.my-class').style.display = 'none';
element.closest('.my-class').nextSibling.style.display = 'block';
}
When I click on the button, why can I hide the first element but cannot show the next div element with the class my-class
. Instead, I get the following error:
Uncaught TypeError: Cannot set property 'display' of undefined
It seems like I cannot select the next div element of the class my-class
using the nextSibling property. What did I do wrong?