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

152
Visualizações
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
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