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

225
Vistas
Show/hide elements with ajax

I am using AJAX to load a list of comic chapters on click. It works fine. I would like to modify the script to hide the chapters when I click again on the same book. Hopefully the below pictures will make it clear.

How it works:


How I want it:

The code to load the chapters is as follows:

function aload(element) {
    var url = element.getAttribute('data-ajax-url');
    var xhr = new XMLHttpRequest();
    xhr.open("POST", url);
    xhr.setRequestHeader("X-CSRFToken", csrftoken);
    xhr.onreadystatechange = function() {
        if (xhr.readyState === 4){
            document.getElementById("chapter-container").innerHTML = this.response;
        }
    }
    xhr.send();
}

Thank you for all your suggestions.

about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

Thanks @Barmar for your suggestions, I have finally implemented a working solution.

var previousValue;
function compare(element) {
    var currentValue = element.dataset.bookid;
    var chapterList = document.getElementById("chapter-container")

    if (previousValue == currentValue && chapterList.style.display == 'block'){
        chapterList.style.display = 'none';
    } else{
        aload(element);
    }
    previousValue = currentValue;
}
about 4 years ago · Santiago Gelvez 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