Conditions: How expand the parent accordion for the current category I'm in?

I have a sidebar with an accordion menu where I display all categories and subcategories, tell me how can I expand the element of the parent category if I am in one of the child categories?

Example

I have the same question. It should be possible through js. Can any enthusiastic person share the js code?