suzzane2020 Posted April 11, 2007 Share Posted April 11, 2007 I have a function that reads elements from an xml file and tries to populate a select box with the elements. The problem is that if i add the line document.myform.subcat.options[count]=newop; [which is the one that generates the select box] the loop just executes once , but i have more elements. Without the line the loop is executed succesfully. cant figure out whats wrong. Please help?!!! This is the code [size=10pt]function get_xml(xmlfile) { var xmlDoc = xmlfile.documentElement; var xSel1 = xmlDoc.getElementsByTagName('result'); var loop=xSel1.length; var count=0; for(var number=0;number<loop;number++) { var theText = xSel1[number].attributes.getNamedItem("name").value; alert(theText) var theValue = xSel1[number].attributes.getNamedItem("value").value; //alert(theValue) var newop= new Option(theText,theValue); //document.myform.subcat.options.add(newop,null); document.myform.subcat.options[count]=newop; count++ } } [/size] Quote Link to comment Share on other sites More sharing options...
nogray Posted April 11, 2007 Share Posted April 11, 2007 I tried this line in a simple loop and it worked fine document.myform.subcat.options[count]=newop; check if your loop and number variables are working through out the loop, or if there is anything else that's generating an error (you can use firefox error councel) Here is the function I tried <script> function loadit(){ var count=0; for(var number=0;number<5;number++) { var theText = "Hello World " + number; var theValue ="Hello World " + number; var newop= new Option(theText,theValue); document.myform.subcat.options[count]=newop; count++ } } </script> <form name="myform" id="myform"> <select name="subcat" id="subcat"> </select> </form> <button onClick="loadit()">Click</button> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.