I've found this code on a website called w3schools and it will filter any text values that matches the value of the input field here's the example below.
$(document).ready(function(){
$("#myInput").on("keyup", function() {
var value = $(this).val().toLowerCase();
$("#myTable tr").filter(function() {
$(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
});
});
});
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<input id="myInput" type="text" placeholder="Search..">
<br><br>
<table>
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Date</th>
</tr>
</thead>
<tbody id="myTable">
<tr>
<td>Johnny</td>
<td>Depp</td>
<td>2022-06-03</td>
</tr>
<tr>
<td>Angelina</td>
<td>Jolie</td>
<td>2022-06-04</td>
</tr>
<tr>
<td>Peter</td>
<td>Parker</td>
<td>2022-06-05</td>
</tr>
<tr>
<td>Lebron</td>
<td>James</td>
<td>2022-06-06</td>
</tr>
</tbody>
</table>
</body>
</html>
My question is can we do this on input date? i just wanted to search dates i tried to change the type to date but it just doesn't work, is there any ways to do this?
Ex:
<input id="myInput" type="date" placeholder="Search..">
$(document).ready(function(){
$("#myInput").on("keyup", function() {
var value = $(this).val().toLowerCase();
$("#myTable tr").filter(function() {
$(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
});
});
});
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<input id="myInput" type="date" placeholder="Search..">
<br><br>
<table>
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Date</th>
</tr>
</thead>
<tbody id="myTable">
<tr>
<td>Johnny</td>
<td>Depp</td>
<td>2022-06-03</td>
</tr>
<tr>
<td>Angelina</td>
<td>Jolie</td>
<td>2022-06-04</td>
</tr>
<tr>
<td>Peter</td>
<td>Parker</td>
<td>2022-06-05</td>
</tr>
<tr>
<td>Lebron</td>
<td>James</td>
<td>2022-06-06</td>
</tr>
</tbody>
</table>
</body>
</html>
CodePudding user response:
it works fine if you change the value with the keyboard
if you want it to run when selecting a date then change it with the 'change' event : same as
$(document).ready(function(){
$("#myInput").on("change", function() {
let value = $(this).val().toLowerCase();
console.log(value)
$("#myTable tr").filter(function() {
$(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
});
});
});
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<input id="myInput" type="date" placeholder="Search..">
<br><br>
<table>
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Date</th>
</tr>
</thead>
<tbody id="myTable">
<tr>
<td>Johnny</td>
<td>Depp</td>
<td>2022-06-03</td>
</tr>
<tr>
<td>Angelina</td>
<td>Jolie</td>
<td>2022-06-04</td>
</tr>
<tr>
<td>Peter</td>
<td>Parker</td>
<td>2022-06-05</td>
</tr>
<tr>
<td>Lebron</td>
<td>James</td>
<td>2022-06-06</td>
</tr>
</tbody>
</table>
</body>
</html>