I have a javascript code that dynamically creates a select box with data from a file. They are a bit big and I am going to try and move then to am external js rather in the HTML once everything is working ok. My problem is that it seems when I try to run the same script, on a different file of course, it clears out the info from the first script. So I wind up with one select element populated and one blank. the last script is the only one that holds, which is why I think it is somehow clearing out the first one. Any suggestions. I am buidling up a menu so I needs this code to be modular. Thanks in advance.
* UPDATE * I have split out the processcsv file and I still get the same results. In each fall of the function i am passing a different select element. On the webpage I will only see the last one execute with the select processed earliar rendered blank. I think the issue is the that my first xmlrequest never achieves readystate == 4 but the last one does. I am not sure what I can do to control that and why is it all but the last one that fails.
my html
<div class="menu_container">
<div class="menu_element" id="plant_div">
<select class="Menu" id="plant_select">
<script>
<!--var plant_select = document.createElement("select"); -->
var datafile = '';
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET","http://localhost:8080/res/plants.csv",true);
xmlhttp.send();
xmlhttp.onreadystatechange = function()
{
if(xmlhttp.status==200 && xmlhttp.readyState==4)
{
processCSV(xmlhttp.responseText, plant_select,"select plant");
}
}
</script>
</select>
</div>
<div class="menu_element" id="plantType_div">
<select class="Menu" id="plantType_select">
<script>
<!--var plant_select = document.createElement("select"); -->
var datafile = '';
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET","http://localhost:8080/res/planttypes.csv",true);
xmlhttp.send();
xmlhttp.onreadystatechange = function()
{
if(xmlhttp.status==200 && xmlhttp.readyState==4)
{
processCSV(xmlhttp.responseText, plantType_select,"select PLant Type);
}
}
</script>
</select>
</div>
</div>
a seperate js file that contains
function processCSV(file,parentNode,defaultmessage)
{
var frag = document.createDocumentFragment()
, lines = file.split('\n'), option;
var intial_option = document.createElement("option");
intial_option.setAttribute("value","");
intial_option.setAttribute("disabled","disabled");
intial_option.setAttribute("selected","selected");
intial_option.innerHTML = defaultmessage;
frag.appendChild(intial_option)
for (var i = 0, len = lines.length; i < len; i++){
option = document.createElement("option");
option.setAttribute("value", lines[i]);
option.innerHTML = lines[i];
frag.appendChild(option);
}
parentNode.appendChild(frag);
}
plant_selectreally have an html comment wrapped around it? If not, when is it ever added to the DOM?processCSV()duplicate?