Skip to content
Advertisement

add class to next sibling element In Javascript

How could I add toggle class? When I click on anchor tag it should add class to only next sibling element ( .treeUlChild ). i tried a lot and try to find solution but couldn’t. I am new and this is my first project in javascript. here is my html code.

JavaScript

This is my javascript code.

JavaScript

Advertisement

Answer

There is very simple way using Bootstrap by the way. But if you want to do that with pure Javascript, you’re on the right way to it.

So first, transform your query selector into a object e.g:

JavaScript

forEach method, querying the single object clicked in the array of multiple objects:

JavaScript

Add functions to your elements:

JavaScript

ps: you can simplify this.

Structure your function:

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