everything i keep trying breaks it.
Help me close the other menus when one is clicked.
https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_collapsible
the maxHeight is confusing me with the nextElementSibling
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
sry bout the lorem, it said my question was too short
2
Answers
You just makes it difficult, just think simply .
Use classes (I mean class in html) just add a unique class to opened one and when is other one is clicked remove this class , you certainly know
document.getElementsByClassName()
And remove it , also in clicked element
Just add event listener or onClick event
Add the class
It’s that easy!
Post-Note: This is by no means the optimal or the best way to do this. It’s just what I believe is the easiest for someone to understand.
Let’s start off by making a function
closeOtherCollapsibles()
that wraps up all the functionality to do just that. This way it’ll be easier to understand.However before that, we need to identify where it should go. Looking at the W3Schools code, this spot seems best
Our function
closeOtherCollapsibles()
is also going to be pretty simple. It needs to do two things.active
classWe do this by iterating over all collapsibles, and just excluding our current one from these changes.
And here’s just that in full action.