I have an HTML table with a search field to filter the result by Name. And another field to show the Country tha correspond to the person.
Everything works fine, but when I remove the input from the search field the Country Field gets the country from last row. What I want is that, whenever the search field is empty, the country field should be empty, too.
this is the code:
function myFunction() {
var input, filter, table, tr, td, i, txtValue;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
table = document.getElementById("myTable");
tr = table.getElementsByTagName("tr");
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName("td")[0];
if (td) {
txtValue = td.textContent || td.innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
document.getElementById("Res").value = tr[i].getElementsByTagName("td")[1].textContent; //traer nombre del pais indice 1
} else {
tr[i].style.display = "none";
}
}
}
}
* {
box-sizing: border-box;
}
#myInput {
background-image: url('/css/searchicon.png');
background-position: 10px 10px;
background-repeat: no-repeat;
width: 100%;
font-size: 16px;
padding: 12px 20px 12px 40px;
border: 1px solid #ddd;
margin-bottom: 12px;
}
#myTable {
border-collapse: collapse;
width: 100%;
border: 1px solid #ddd;
font-size: 18px;
}
#myTable th,
#myTable td {
text-align: left;
padding: 12px;
}
#myTable tr {
border-bottom: 1px solid #ddd;
}
#myTable tr.header,
#myTable tr:hover {
background-color: #f1f1f1;
}
My Customers
Alfreds Futterkiste |
Germany |
Berglunds snabbkop |
Sweden |
Island Trading |
UK |
Koniglich Essen |
Germany |
Laughing Bacchus Winecellars |
Canada |
Magazzini Alimentari Riuniti |
Italy |
North/South |
UK |
Paris specialites |
France |
Run code snippetExpand snippet
JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)