html - Value of list populated with javascript -
how populate select list values got javascript?
i sending request .php site gives me respond in json format. want put lines got select list.
<select id = "list" name=log size=50 style=width:1028px> <script type="text/javascript"> window.onload = function () { var bytes=0; var url = "/test/log.php?q='0'"; function httpget(url) { var xhttp = new xmlhttprequest(); var realurl = url + bytes; xhttp.open("get", url, true); xhttp.onload = function (e) { if (xhttp.readystate === 4) { if (xhttp.status === 200) { console.log(xhttp.responsetext); var response=json.parse(xhttp.responsetext); var log = response.key; bytes = log.length; } }; xhttp.onerror = function (e) { console.error(xhttp.statustext); } }; xhttp.send(); } var updateinterval = 2000; function update() { httpget(url); settimeout(update, updateinterval); } update(); } </script> </select>
the response log.php
"{"key":"whole log file"}"
. want store reponse list , populate every 2 seconds.
loop on contents of returned string after json.parse
-ing it. create option elements it, , insert select.
var html = ""; var obj = json.parse(xhttp.responsetext); for(var key in obj) { html += "<option value=" + key + ">" +obj[key] + "</option>"; } document.getelementbyid("list").innerhtml = html;
see jsbin
Comments
Post a Comment