user987013
user987013

Reputation: 243

Having trouble input html list option from xml file

Please help take a look at the following code, its working when calling function displayResult() with button onclick. but I want it to be automatically run when open the page....I've tried but still not working....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>

Upvotes: 0

Views: 813

Answers (1)

nnnnnn
nnnnnn

Reputation: 150080

You need to call displayResult() after the elements it manipulates have been parsed. So add the following code to a script block at the bottom of your page source (or in an onload or document.ready event handler):

displayResult();

Upvotes: 1

Related Questions