I'm using a html input field to search for results from OMDb, currently I just have them shown below the form in a list, but would like them to appear below the input field, kinda like when you're on Google's homepage and you get a few results, where it drops down showing potential search results from what you've typed already (ie if I type Shang it might pop down below it showing Shang-Chi, Shei xian ai shang ta de, Hai shang hua, Da Shang Hai etc..).
<li>
<label for="search">OMDb lookup:</label>
<input id="search" placeholder="start typing film title" type="input" name="search" maxlength="255" onkeyup="waitForIt()">
</li>
<li>
<ul id="omdbReturn">
</ul>
</li>
Is what I have for the HTML, then my javascript is:
var timer = null;
function waitForIt() {
clearTimeout(timer);
timer = setTimeout(omdbSearch, 1000)
};
function omdbSearch() {
let searchTerm = encodeURI(document.getElementById("search").value);
$.getJSON('https://www.omdbapi.com/?apikey=<apikey>&type=movie&page=1&s=' + searchTerm).then(function(omdbResults) {
if (omdbResults.Response !== 'False') {
let tot = omdbResults.totalResults;
if (tot > 10) { //only return up to first ten results
tot = 10;
}
document.getElementById('omdbReturn').innerHTML = '';
for (i = 0; i < tot; i++) {
document.getElementById('omdbReturn').innerHTML += ('<li id="' + omdbResults.Search[i].imdbID + '">' + omdbResults.Search[i].Title + ' (' + omdbResults.Search[i].Year + ')</li>');
}
let list = document.getElementById("omdbReturn");
function Populate() {
$.getJSON('https://www.omdbapi.com/?apikey=<apikey>&type=movie&plot=full&i=' + this.id).then(function(omdbMovie) {
document.getElementById("film").value = omdbMovie.Title + " (" + omdbMovie.Year + ")";
document.getElementById("image_url").value = omdbMovie.Poster;
document.getElementById("desc").value = omdbMovie.Plot;
document.getElementById("imdb").value = "https://www.imdb.com/title/" + omdbMovie.imdbID + "/";
document.getElementById('omdbReturn').innerHTML = '';
});
}
for (i = 0; i <= list.childElementCount - 1; i++) {
list.children[i].addEventListener("click", Populate);
}
} else {
document.getElementById('omdbReturn').innerHTML = '';
}
});
}
So this all works great, produces a nice little list of results and if I click on a result it will populate the relevant fields in the rest of my form (saving the user having to go to IMDb and copy/paste the results themselves) - I would just like to improve how it appears on screen but my CSS is terrible and google isn't really understanding what I'm trying to ask (so lacking the wording I guess to nail the search).
Not looking for someone to bespokely script it for me, just pointers in the right direction would be greatly appreciated!
[edit]
Just a thought, here's what the Javascript injected HTML looks like, using the "Shang" search example again:
<ol id="omdbReturn">
<li id="tt9376612">Shang-Chi and the Legend of the Ten Rings (2021)</li>
<li id="tt8443704">Shei xian ai shang ta de (2018)</li>
<li id="tt0156587">Hai shang hua (1998)</li>
<li id="tt2169322">Bei Jing yu shang Xi Ya Tu (2013)</li>
<li id="tt6628322">Shang hai bao lei (2019)</li>
<li id="tt2458314">Da Shang Hai (2012)</li>
<li id="tt0492473">Kan shang qu hen mei (2006)</li>
<li id="tt0101864">Fei zhou he shang (1991)</li>
<li id="tt9081562">Bi bei shang geng bei shang de gu shi (2018)</li>
<li id="tt11448076">Xuan ya zhi shang (2021)</li>
</ol>