Having a bit of a problem looping through the csv file. I am also not sure if there isn't a simpler way of loading the text file. I know a for each loop makes sense but I am not sure about the individual item in a csv file. I want the whole line of text and I assign the two piece of text to the value and choice parts of the option. Any advice to clean this up?
*UPDATE incorporating the suggestions below. Error free but the created element is not being captured by my CSS file so the formatting is off and the select box only shows blanks.
<script>
function processCSV(file,parentNode)
{
var frag = document.createDocumentFragment()
, lines = file.split('\n'), 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);
}
plant_select.appendChild(frag);
}
var plant_select = document.createElement("select");
var intial_option = document.createElement("option");
var datafile = '';
var xmlhttp = new XMLHttpRequest();
plant_select.setAttribute("class", "selectbox");
plant_select.setAttribute("id", "plant_select");
intial_option.setAttribute("value","")
intial_option.setAttribute("disabled","disabled")
intial_option.setAttribute("selected","selected")
intial_option.innerHTML = "Please select a Plant";
plant_select.appendChild(intial_option)
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);
}
}
</script>