Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

138
Vistas
Java Script accordion functionality problem

Im making a app in django which needs a accordion. This is the following HTML code

{% load static %}
{% for comp in comps %}


<script src="{% static 'main/app.js' %}"></script>

<div class="accordion-body">
    <div class="accordion">

    <div class="container">
        <div class="label">
        {{comp.name}}
        {{comp.country}}, {{comp.city}}
        </div>

        <div class="content">{{comp.descr}}</div>
        <hr>

    </div>

  </div>

</div>
{% endfor %}

This is the css which actually hides the content. Note Ive taken out a lot of uneccessary css like font-size and colors.

.accordion .label::before {
    content: '+';
    top: 50%;
    transform: translateY(-50%);
  }
  
  
  .accordion .content {
    text-align: justify;
    width: 780px;
    overflow: hidden;
    transition: 0.5s;
  }

  .accordion .container.active .content {
    height: 150px;
  }

Finally this is javascript which is meant to add a Event Listener

const accordions = document.getElementsByClassName('container');

Array.from(accordions).forEach((accordion)=>{
   accordion.addEventListener('click', function() {
    this.classList.toggle('active')
  })
})

The active class isnt being added.

There might be a problem with linking the js with the HTML doc since when I run the app this is what I get in the command line. GET /static/main/app.js HTTP/1.1 The location is definitely correct

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

getElementsByClassName returns a list of elements.

let containers = document.getElementsByClassName('container');

for(let i = 0; i < containers.length; i++) {
  containers[i].addEventListener("click", function() {
    this.classList.toggle('active')
  })
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

There are several points to be noticed in your source code setup, each to be handled.

  • Your <script>... tag is inside the django {% for ... %} loop, means the same script is being loaded multiple times (depending upon size of your comp). Move your <script>... out of the loop. This will also prevent multiple declaration errors, like accordions has already been declared.

  • Your <script>... tag is loaded before the container class div, and the script is not waiting for document to load, so your document.getElementsByClassName('container') won't be getting anything anyway. Try adding this function call inside a document load event listener in your script, or position your script load at the bottom of your html code (preferably after the container class div).

  • The accordion variable is a list, and not a single HTMLElement object. Therefore, the addEventListener('click'... won't work. Try this

...
const accordions = document.getElementsByClassName('container');

Array.from(accordions).forEach((accordion)=>{
   accordion.addEventListener('click'...//rest of your code
   ...
})
...
  • You have not included the stylesheet in html (if you have taken that part out here only then its alright).

There could be other problems as well apart from the ones I noticed from you currently provided code. But these points should help you too.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda