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

114
Visualizações
Clear search bar

I want to clear a search bar. The idea was to search through a list in a table. A search function is called onkeyup from an input then I added an icon eraser that clears the input onclick (span).

Even though the input is cleared, the search is still displayed with the list of result from the search. I would like to get back to the whole list on the click of the eraser icon. Any suggestion?

function search() {
  var input, filter, found, table, tr, td, i, j;
  input = document.getElementById("searchInput");
  filter = input.value.toUpperCase();
  table = document.getElementById("list");
  tr = table.getElementsByTagName("tr");

  for (i = 0; i < tr.length; i++) {
    td = tr[i].getElementsByTagName("td");
    for (j = 0; j < td.length; j++) {
      if (td[j].innerHTML.toUpperCase().indexOf(filter) > -1) {
        found = true;
      }
    }
    if (found) {
      tr[i].style.display = "";
      found = false;
    } else if (!tr[i].id.match('^tableHeader')) {
      tr[i].style.display = "none";
    }
  }
};
<div class="search-box pull-left">
  <form action="">
    <input type="search" id="searchInput" onkeyup="search()" placeholder="Search..." title="Type in anything">
    <span id="clearSearch" onclick="document.getElementById('searchInput').value = null;">
            <i class="ti-eraser"></i>
        </span>
  </form>
</div>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You mean adding search to the event handler of the clear?

Note I moved the inline event handlers and changed to the better "input" event since it handles paste too and changed the null to an empty string

It would have been easier to test if I had your table

window.addEventListener("DOMContentLoaded", function() {
  document.getElementById("clearSearch").addEventListener("click", function() {
    document.getElementById('searchInput').value = "";
    search();
  });
  document.getElementById("searchInput").addEventListener("input", search)  
});

const search = () => {
  var input, filter, found, table, tr, td, i, j;
  input = document.getElementById("searchInput");
  filter = input.value.toUpperCase();
  table = document.getElementById("list");
  tr = table.getElementsByTagName("tr");

  for (i = 0; i < tr.length; i++) {
    td = tr[i].getElementsByTagName("td");
    for (j = 0; j < td.length; j++) {
      if (td[j].innerHTML.toUpperCase().indexOf(filter) > -1) {
        found = true;
      }
    }
    if (found) {
      tr[i].style.display = "";
      found = false;
    } else if (!tr[i].id.match('^tableHeader')) {
      tr[i].style.display = "none";
    }
  }
};
window.addEventListener("DOMContentLoaded", function() {
  document.getElementById("clearSearch").addEventListener("click", function() {
    document.getElementById('searchInput').value = "";
    search();
  });
  document.getElementById("searchInput").addEventListener("input", search)  
});
<div class="search-box pull-left">
  <form action="">
    <input type="search" id="searchInput"  placeholder="Search..." title="Type in anything">
    <span id="clearSearch">
            <i class="ti-eraser"></i>
        </span>
  </form>
</div>

<table id="list"><tbody>
<tr><td>Some text</td></tr> 
</tbody></table>

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