Skip to content
Advertisement

Using the nextSiblingElement in forEach function after clicking again is not working

I am practicing making FAQ accordion, I am able to show the answer after

clicking the question but when I am clicking again to hide the answer it is not working.

JavaScript
JavaScript

It is showing the nextElementSibling when clicking the first time but on the second time it does not do anything not even throw the error.

Advertisement

Answer

The problem is with the condition where you are using if (next.style.display = 'none') {}.

Technically there are two problems, first is next.style.display which gives nothing so instead of that use getComputedStyle function to get the value of any property in this case ‘display’, thats because Computed style contains all the CSS properties set to an element. Even if do not set a property to an element. You will still find that property in the computed styles.

Second is a minor typo in the if condition = which should be ==

Working Example:

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement