var Model,node;
document.getElementById('sedans').innerHTML='';
var thismodelabbr,prevmodelabbr;
for(var j=0; j<xmlDoc.getElementsByTagName('data').length; j++){
node = xmlDoc.getElementsByTagName('data')[j];
thismodelabbr=node.getAttribute('model');
if(prevmodelabbr!=thismodelabbr){
Model+='<a href="">'+ node.getAttribute('model')+'</a>';
}
prevmodelabbr=thismodelabbr;
document.getElementById('sedans').innerHTML=Model;
}
The above javascript snippet is working correctly and as needed, but I’m getting an “Undefined” response before the entry is displayed within its respective page. I’m assuming it has to do with the .innerHTML call. Any insight would be deeply appreciated.
Modelstarts out as an uninitialized variable, containingundefined. In the loop you append strings to it with+=, so in the first iteration the first string is “appended” to the initialundefinedvalue. To do this javascript convertsundefinedto a string and appends the new string to it, so that you end up with a string starting with “undefined”.If you initialize
Modelwith an empty string the issue should go away: