i want to filter or search in this table by using any columns such Title or start date or visible and so on now my code search by first col "Title"only i can't make it search in all cols .html that is exactly what i want
<div>
<label>
Search:<input type="text" id="Search" onkeyup="myFunction()" placeholder="search for performance">
</label>
</div>
<table id="PerformanceTable" class="table table-bordered table-striped">
<tr>
<th>
Title
</th>
<th>
Start Date
</th>
<th>
Booking
</th>
<th>
Visible
</th>
<th>
Featured
</th>
<th>
Event
</th>
<th>
@Html.DisplayNameFor(model => model.Views)
</th>
<th></th>
</tr>
</table>
<script>
function myFunction() {
// Declare variables
var input, filter, table, tr, td, i;
input = document.getElementById("Search");
filter = input.value.toUpperCase();
table = document.getElementById("PerformanceTable");
tr = table.getElementsByTagName("tr");
// Loop through all table rows, and hide those who don't match the search query
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName("td")[0];
if (td) {
if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
}
}
}</script>
i want to filter or search in this table by using any columns such Title or start date or visible and so on now my code search by first col "Title"only i can't make it search in all cols https://datatables/examples/data_sources/dom.html that is exactly what i want
<div>
<label>
Search:<input type="text" id="Search" onkeyup="myFunction()" placeholder="search for performance">
</label>
</div>
<table id="PerformanceTable" class="table table-bordered table-striped">
<tr>
<th>
Title
</th>
<th>
Start Date
</th>
<th>
Booking
</th>
<th>
Visible
</th>
<th>
Featured
</th>
<th>
Event
</th>
<th>
@Html.DisplayNameFor(model => model.Views)
</th>
<th></th>
</tr>
</table>
<script>
function myFunction() {
// Declare variables
var input, filter, table, tr, td, i;
input = document.getElementById("Search");
filter = input.value.toUpperCase();
table = document.getElementById("PerformanceTable");
tr = table.getElementsByTagName("tr");
// Loop through all table rows, and hide those who don't match the search query
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName("td")[0];
if (td) {
if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
}
}
}</script>
Share
Improve this question
edited Jan 19, 2017 at 18:44
Carsten Løvbo Andersen
27.1k10 gold badges50 silver badges79 bronze badges
asked Jan 17, 2017 at 16:01
amal mansouramal mansour
3652 gold badges5 silver badges16 bronze badges
7
-
1
You have to iterate trough
tr[i].getElementsByTagName("td")
collection – hindmost Commented Jan 17, 2017 at 16:06 - i have 2 errors now 1) can't read uppercase because of date 2) can't read indexof() @hindmost – amal mansour Commented Jan 17, 2017 at 16:17
- Show your changes – hindmost Commented Jan 17, 2017 at 16:26
- only delete [0] now for (i = 0; i < tr.length; i++) { td = tr[i].getElementsByTagName("td"); @hindmost – amal mansour Commented Jan 17, 2017 at 16:27
- it make error in function uppercase and index of – amal mansour Commented Jan 17, 2017 at 16:44
2 Answers
Reset to default 5Remove the .getElementsByTagName("td")[]
the for loop should be like this:
for (i = 1; i < tr.length; i++) {
td = tr[i];
if (td) {
txtValue = td.textContent || td.innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
}
}
You need to loop through all the td
s available. But make sure to break when you find the match. Example code below:
// Declare variables
var input, filter, table, tr, td, i ;
input = document.getElementById("Search");
filter = input.value.toUpperCase();
table = document.getElementById("PerformanceTable");
tr = table.getElementsByTagName("tr"),
th = table.getElementsByTagName("th");
// Loop through all table rows, and hide those who don't match the search query
for (i = 1; i < tr.length; i++) {
tr[i].style.display = "none";
for(var j=0; j<th.length; j++){
td = tr[i].getElementsByTagName("td")[j];
if (td) {
if (td.innerHTML.toUpperCase().indexOf(filter.toUpperCase()) > -1) {
tr[i].style.display = "";
break;
}
}
}
}