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

215
Visualizações
How to show a message when no results are found

How would I adapt this code to show a <div>No Results Found</div> on the page when no results are found?

I've got this simple image gallery with an input to filter results based on the contents of the data-filter attribute:

<input type="text" name="filter" placeholder="Filter" id="filter">
<div class="results-gallery">
    <div class="gallery-thumbnail" data-filter="Apple">image</div>
    <div class="gallery-thumbnail" data-filter="Orange">image</div>
    <div class="gallery-thumbnail" data-filter="Banana">image</div>
</div>

And the jQuery:

$("#filter").keyup(function() {
    var value = $(this).val().toLowerCase();
    $(".gallery-thumbnail").filter(function() {
        $(this).toggle($(this).attr("data-filter").toLowerCase().indexOf(value) > -1);
    });
});
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You could use the same method and do it like this:

$(document).ready( function(){
    $("#filter").keyup( function(){
        var value = $(this).val().toLowerCase();
        $(".gallery-thumbnail").filter(function() {
            $(this).toggle($(this).attr("data-filter").toLowerCase().indexOf(value) > -1);
        });
        $('.noresult').toggle($(".gallery-thumbnail:visible").length == 0)
    });
});

Demo

$(document).ready( function(){
    $("#filter").keyup( function(){
        var value = $(this).val().toLowerCase();
        $(".gallery-thumbnail").filter(function() {
            $(this).toggle($(this).attr("data-filter").toLowerCase().indexOf(value) > -1);
        });
        $('.noresult').toggle($(".gallery-thumbnail:visible").length == 0)
    });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" name="filter" placeholder="Filter" id="filter">
<div class="results-gallery">
    <div class="gallery-thumbnail" data-filter="Apple">image</div>
    <div class="gallery-thumbnail" data-filter="Orange">image</div>
    <div class="gallery-thumbnail" data-filter="Banana">image</div>
    <div class="noresult" style="display:none">No Results Found</div>
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

You put 'No Results' div on the page, with a class which initially defaults it to be hidden (display:none). If your filter returns no results, you add a class to the div to show it (display: block). When the filter is cleared, and you have results again, you remove the 'show' class, reverting it back to it's default 'hidden' state.

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