I am trying to create an autocomplete search box.
It works, but there is one problem here is that it won't start with the first letter when user type on the search box.
If I type the first letter on the search box, it will show all words contain this letter, but I only want it show words start with this letter.
What should I do? I found out this happen in firefox, but it is OK in chrome.
var dataList = document.getElementById("auto_json_datalist");
// Create a new XMLHttpRequest.
var request = new XMLHttpRequest();
// Handle state changes for the request.
request.onreadystatechange = function(response) {
if (request.readyState === 4) {
if (request.status === 200) {
// Parse the JSON
var jsonOptions = JSON.parse(request.responseText);
// Loop over the JSON array.
jsonOptions.forEach(function(item) {
// Create a new <option> element.
var option = document.createElement("option");
// Set the value using the item in the JSON array.
option.value = item;
// Add the <option> element to the <datalist>.
dataList.appendChild(option);
});
} else {
// An error occured :(
}
}
};
// Set up and make the request.
request.open("GET", "autocompleteSym.json", true);
request.send();
<datalist id="auto_json_datalist"></datalist>
<input type="text" class="enter_sym_c" list="auto_json_datalist" value="" />