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

122
Visualizações
Conditonal Modal in JS

I need help creating a conditonal pop up modal when no results are found from an API call.

When the API returns a result: length:0 from the array I would like a modal to appear that says no results found.

The CSS is working. This is my HTML + JS

HTML

        <div class="modal" id="myModal">
          <div class="modal-content">
          <span class="close">&times;</span>
          <p>Sorry no results found</p>
        </div>
        </div>
        <button class="btn" id="searchBtn">Search</button>

JS

var modal = document.getElementById("myModal");
var btn = document.getElementById("searchBtn");
var span = document.getElementsByClassName("close")[0];

btn.onclick = function(){
// for (var i = 0; i < data.length; i++) {
// if (data[i].length === 0) {
modal.style.display = "block";
};

span.onclick = function(){
modal.style.display = "none";
};

window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
};

API call code

   var newRecipe = 
   'https://api.spoonacular.com/recipes/findByIngredients?ingredients='+ 
   allIngreds + '&number=10&apiKey=39791063581a4d96a908bb19745b3f64';

   fetch(newRecipe)
  .then(response => {
     if (!response.ok){
        throw Error("ERROR")
     };
     return response.json();
    })
   .then(data => {
   console.log(data);
   });
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Based on the api call that you posted, it should be sth like this

   fetch(newRecipe)
  .then(response => {
     if (!response.ok){
        throw Error("ERROR")
     };
     return response.json();
    })
   .then(data => {
   console.log(data);
   if (data.length === 0) {
     modal.style.display = "block";
   } else {
     modal.style.display = "none";
   }
   });
about 4 years ago · Juan Pablo Isaza 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