Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


JavaScript accordion, expand divs when clicked

This is probably pretty simple but I'm a newbie and can't find the answer online :)

I'm trying to create a series of panels that should expand when clicked, kinda like an accordion menu but the whole div is clickable, also it should retract when clicked again. I've figured out how to get one panel to expand and retract but I don't know how to make it work for several divs. I'm guessing some kind of for-loop is needed, but can't understand how.

var panel = document.getElementById("panel-id");

panel.addEventListener("click", function(){
  if (panel.style.height === "100px") {
  panel.style.height = "400px";
  } else {
    panel.style.height = "100px"

1 Answer

Hey, you can check out a basic tutorial here.

Also IDs should always be unique, so only use them once.