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

113
Views
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 answers
Answer question

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 Report
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!