Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


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.