Having trouble entering html list list from xml file

Please help take a look at the following code, its work when calling the displayResult () function with the onclick button. but I want it to start automatically when the page opens .... I tried, but still did not work .... Thanks

<form> <select id="mySelect"> <option>SELECT</option> </select> </form> <script language="JavaScript" type="text/javascript"> if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.open("GET","xmltag.xml",false); xmlhttp.send(); xmlDoc=xmlhttp.responseXML; x2=xmlDoc.getElementsByTagName("CONTROL_POINT"); function displayResult() { for (i=0;i<x2.length;i++) { var x=document.getElementById("mySelect"); var option=document.createElement("option"); option.text=x2[i].childNodes[0].nodeValue; try { // for IE earlier than version 8 x.add(option,x.options[null]); } catch (e) { x.add(option,null); } } } </script> 
0
source share
1 answer

You need to call displayResult() after the elements that it processes have been parsed. Therefore, add the following code to the script block at the bottom of the page source (or in the onload or document.ready event handler):

 displayResult(); 
+1
source

Source: https://habr.com/ru/post/1498605/


All Articles