Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

231
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!