javascript - Fixed - AJAX and datalists -


i'm trying use xml list of cities website, go through , add each of cities datalist when put in input filter cities in list.

example of city list:

aleppo alexandria alger almaty ankara anshan baghdad baku bandung bangalore bangkok barcelona 

*[each city name on new line]

current html:

<div id="namearea">         <h2>city name:</h2>          <div>             <input id="citiesinput" list="cities">             <datalist id="cities"></datalist>              <button id="search">                 search             </button>              <span class="loading" id="loadingnames">                 loading...             </span>         </div>     </div> 

current js code:

window.onload = function() {     var request = new xmlhttprequest();     request.onload = processcities;     request.open("get", "url", true);     request.send(); }; 

inspecting page firebug shows nothing being added datalist, i'm wondering i'm doing wrong. tried using .responsetext rather .responexml didn't make difference. can me?

[edit] progress has been made. changed processcities() function to:

function processcities() {     var response = this.responsetext;     var city = response.split("\n");     var options = "";      for(var = 0; < response.length; i++) {         options += "<option value='"+city[i]+"'>\n";     }     document.getelementbyid("cities").innerhtml = options; } 

this code seems work.

thanks help.

here example of making request. if getting xml, you'll have parse it. it's better if json.

var request = new xmlhttprequest();  request.open('get', '/my/url', true);    request.onload = function() {    if (request.status >= 200 && request.status < 400) {      // success!      var data = json.parse(request.responsetext);    } else {      // reached our target server, returned error      }  };    request.onerror = function() {    // there connection error of sort  };    request.send();


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -