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

151
Views
How can I filter API through clicking on HTML li?

So I have a Google Books API for searching books. I have a searchBar which allows to search books writing any information about them and pass it to function as query:

function searchBook(query){
        const url = BASE_URL + `${query}` + '&' + API_KEY;
    
        fetch(url)
        .then(res => res.json())
        .then(books => showBooks(books.items));
    }

But now I want to make a ul which displays different categories of books, and when I click on one of the li, it displays books of this certain category. Picture with categories ul:

enter image description here

Javascript code for selecting categories when clicking on li:

const list = document.getElementById('list');

        const li = list.querySelectorAll('li');

        for (let i = 0; i < li.length; i++) {
            li[i].addEventListener("click", e =>{
                e.target.innerHTML == "Fiction" ? searchBook('subject:fiction') : console.log('doesnt work');
            })
            
        }

But for some reason nothing happens, books don't display and the console is empty, and there's no errors either.

about 4 years ago · Juan Pablo Isaza
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!