Is there a way to create a search bar similar to those seen on popular websites like YouTube, where the search results overlay the rest of the page without displacing any content? I've searched extensively on Google and YouTube for tutorials on database searching and search bar creation, but I haven't found one that addresses displaying search results in a dropdown without affecting other elements on the page. Even attempting to use CSS display: none; along with JavaScript to show the results ends up displacing content. Any help or guidance would be greatly appreciated.
I've tried implementing this JavaScript code for searching through a table:
function search() {
var input, filter, found, table, tr, td, i, j;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
table = document.getElementById("myTable");
table.style.display = "block";
tr = table.getElementsByTagName("tr");
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName("td");
for (j = 0; j < td.length; j++) {
if (td[j].innerHTML.toUpperCase().indexOf(filter) > -1) {
found = true;
}
}
if (found) {
tr[i].style.display = "";
found = false;
} else if (!tr[i].id.match('^tableHeader')) {
tr[i].style.display = "none";
}
}
}
And my HTML structure is as follows:
<table id="myTable">
<tr>
{% for key, value in list.items() %}
<td>{{ key|e }</td>
<td id="name">{{ value|e }}</td>
<td>
<form class="form-signin ajax" action="/signIn" method="post" data-replace="#res" role="form">
<input type="checkbox" name="watchlist" value="Watchlist" onclick="writeToCell(this.id)" id="{{ key|e }}">
</form>
</td>
</tr>
{% endfor %}
</table>
I am working within the Flask framework using Python as well.
Initially, I used display: none;
in CSS to hide the table, resulting in the h2 tag being displayed below it.
However, upon typing in the search bar, the table contents displace the h2 tag, remaining on-screen even when not actively selected. This is not the behavior I want when interacting with SQL database results. Any suggestions on how to prevent this displacement issue are welcome. Thank you.