I have following search form in an HTML page on website built with Django:
<form method="get">
<input class="form-control" type="text" name="q" id="id_q" placeholder="Search...">
</form>
I would like it to perform a search while typing (not having to press enter). I know we could use onchange or onkeyup, but I don't seem to be able to make it work.
The javascript at the bottom of the page is as follows:
<script type="text/javascript">
document.getElementById("id_q").value = "{{query}}"
</script>
Edit: I saw that my search works without the Javascript part above, but I still need to press on Enter. What would be a simple script to launch the search "as you type" without pressing Enter?
once you onchange you can get your current values and send a API request or anything you want to do something like below-
window.addEventListener('load', () => {
const input = document.getElementById("id_q");
input.addEventListener('change', (e) => {
let currentVal=e.target.value;
// do anything with this value
})
})