I don't know if it "works" or not, but I am concluding this because the code posted here, works (except for what's in the for loop (stupid DOM), but that's not the focus) ONLY after two clicks. I'm assuming this is because it is not getting the XML document till it's created, which is at the end of the first click.
However, if I were to put XMLhttpRequest.open and XMLHttpRequest.send methods BEFORE the onreadystatechange function, the code will not execute inside the nested if statement (the one that checks readystate/status). I have tested this with alert or document.write.
Any ideas what is going wrong?
if (window.XMLHttpRequest)
{
var xmlhttp = new XMLHttpRequest();
}
function lessonList()
{
var xmldoc = xmlhttp.responseXML;
xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
var getTitle = xmldoc.getElementsByTagName('title');
var lessonList = document.getElementById('lesson-list');
for (i = 0; i < getTitle.length; i++)
{
var lessonTitle = document.createElement('li');
lessonTitle.nodeValue = getTitle[i].childNodes[0].nodeValue;
lessonList.appendChild(lessonTitle);
}
}
};
xmlhttp.open("GET", 'file.xml', true);
xmlhttp.send(null);
}