I would like to implement a search function which uses input to find text on my page without pressing enter and hiding everything else not matching the current input.
Tricky stuff: the text on my page is hidden by default and only visible by hovering its container tiles. Also the entire page is built by scripts. This is the structure:
<div id="data" class="grid-container">
<div class="grid-item">
<div class="inside" id="item0">
<h2 id="contents0" class="content-title" href="some link">some headline</h2>
<div class="collapsing">
<br>
<table>
<tbody>
<tr id="tr0">
<td class="tabledata">
<a class="content" href="some link">some headline</a>
</td>
</tr>
<tr>
<td class="tabledata">
<a class="content" href="some link">some headline</a>
</td>
</tr>
<tr>
<td class="tabledata">
<a class="content" href="some link">some headline</a>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
so there are various grid-item's like this one with different amounts of tabledata's. this is the input:
<input class="search" type="search" placeholder="Search" id="input">
Any ideas on how to do this for my special case? I want to hide the entire grid-item if nothing inside matches the current input and I want to hide only the tabledatas not matching the current input if there are matches with one ore more other tabledatas in the same grid-item.
this is my attempt at editing the suggestion down there:
<script>
function findDom(val) {
if (val === '') {
document.querySelectorAll('.hideElem').forEach(function(item) {
item.classList.remove('hideElem')
})
return;
}
document.querySelectorAll('.content').forEach(function(item) {
if (item.textContent.trim().includes(val)) {
item.classList.remove('hideElem')
}
else {
item.classList.add('hideElem')
}
})
}
document.getElementById('input').addEventListener('keyup', (e) => {
setTimeout(function() {
findDom(e.target.value.trim())
}, 2000)
})
</script>
CSS:
.hideElem {
display: none;
} However it does not work at all...