Hi all,
Apologies I'm a completely beginner in JS. I have the following script that filters a table based on the the text as I type. However, my list will be quite long before anything is typed in the box. Therefore, how can i adjust the code so that the table only starts to appear once something is entered into the text box (without using a separate search button).
-<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {
box-sizing: border-box;
}
#myInput {
background-image: url('/css/searchicon.png');
background-position: 10px 10px;
background-repeat: no-repeat;
width: 100%;
font-size: 12px;
padding: 12px 20px 12px 40px;
border: 1px solid #ddd;
margin-bottom: 12px;
}
#myTable {
border-collapse: collapse;
width: 100%;
border: 1px solid #ddd;
font-size: 10px;
}
#myTable th, #myTable td {
text-align: left;
padding: 26
px;
}
#myTable tr {
border-bottom: 1px solid #ddd;
}
#myTable tr.header, #myTable tr:hover {
background-color: #D9B300;
}
</style>
</head>
<body>
<h2>Search by Building Number</h2>
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Enter any part of your building number..." title="Type in an address">
<table id="myTable">
<tr class="header">
<th style="width:20%;">House Number</th>
<th style="width:20%;">Building Name</th>
<th style="width:60%;">BuildingID</th>
</tr>
<tr><td>G-1000</td><td></td><td>8259-23955-4071</td>
<tr><td>G-1001</td><td></td><td>8280-23955-4041</td>
<tr><td>G-1002</td><td></td><td>8279-23955-4076</td>
<tr><td>G-1003</td><td></td><td>8304-23955-4046</td>
<tr><td>G-1004</td><td></td><td>8291-23955-4079</td>
<tr><td>G-1005</td><td></td><td>8322-23955-4049</td>
<tr><td>G-1006</td><td></td><td>8309-23955-4082</td>
<tr><td>G-1007</td><td></td><td>8338-23955-4053</td>
</table>
<script>
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 = "";
} else {
tr[i].style.display = "none";
}
}
}
}
</script>
</body>
</html>
Many Thanks
Sdg