Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

232
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda