json - Creating Autocomplete Dropdowns with the data comes from database in Javascript -


i want create small web application includes function search movies in javascript. want make when search movie's name, function completes rest of word. couldn't make it.

for example:

i wrote "bat" , function completes "man". google search. wrote "stack" , google completes "overflow" in dropdown list.

so here code in script:

var datalist = document.getelementbyid('json-datalist');  var input = document.getelementbyid('ajax');  var request = new xmlhttprequest();  request.onreadystatechange = function(response) { if (request.readystate === 4) {    var jsonoptions = json.parse(request.responsetext);    jsonoptions.foreach(function(item) {      var option = document.createelement('option');      option.value = item;      datalist.appendchild(option);   });  else {    input.placeholder = "couldn't load datalist options ";   }  } };  request.open('get', 'url', true); request.send(); 

html code:

    <form>        <p> movie name: </p> <input type="text" id="ajax" list="json-datalist" placeholder="e.g. spider-man"> <datalist id="json-datalist"></datalist> 

i have verified above code working correctly. problem how sending data server.

the problem @ below line.

option.value = item; 

check json object, how sending data back. example if there properties in object should use below;

option.value = item.value; => name of element 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -