Reputation: 209
I'm pulling information from XML and placing it into several div's that I'm building with the jquery each() and append() functions.
It works great in all browsers except for IE8. In IE8, the function does not seem to fire at all.
Does anyone know why this is?
var xml = '<questions><question1 text="When was the first solar photovoltaic cell invented?"><choice>1940s</choice><choice answer="correct">1950s</choice><choice>1970s</choice><choice>1980s</choice><correct>Thats right</correct><wrong>It was in the 50s</wrong><correctanswer>The first modern solar cell was created by ATT Bell in 1954. One of the first uses for the technology was on early space satellites like the Vanguard 1.</correctanswer><wronganswer>The first modern solar cell was created by ATT Bell in 1954. One of the first uses for the technology was on early space satellites like the Vanguard 1.</wronganswer></question1></questions>';
xmlDoc = $.parseXML( xml ),
$xml = $( xmlDoc ),
currentQues = 1
curr = 'question' + currentQues;
$question = $xml.find(curr);
$question.find("choice").each(function(){
x = $(this).attr("answer");
ctext = $(this).text();
$(".answers").append("<div class='ans'><input id='" + x + "' type='radio'><label>" + ctext + "</label></div>");
});
Upvotes: 1
Views: 3410
Reputation: 4523
Try this: Fiddle
$(document).ready(function(){
var xml = '<questions><question1 text="When was the first solar photovoltaic cell invented?"><choice>1940s</choice><choice answer="correct">1950s</choice><choice>1970s</choice><choice>1980s</choice><correct>Thats right</correct><wrong>It was in the 50s</wrong><correctanswer>The first modern solar cell was created by ATT Bell in 1954. One of the first uses for the technology was on early space satellites like the Vanguard 1.</correctanswer><wronganswer>The first modern solar cell was created by ATT Bell in 1954. One of the first uses for the technology was on early space satellites like the Vanguard 1.</wronganswer></question1></questions>';
var appendtext = "";
xmlDoc = $.parseXML( xml ),
$xml = $( xmlDoc ),
currentQues = 1
curr = 'question' + currentQues;
$question = $xml.find(curr);
$question.find("choice").each(function(){
x = $(this).attr("answer");
if (typeof x !== 'undefined' && x !== false) {
ctext = $(this).text();
appendtext = "<div class='ans'><input id='" + x + "' type='radio'><label>" + ctext + "</label></div>";
}
});
$( ".answers" ).append(appendtext );
});
Explanation: You have to check if x
is undefined. Meaning check for <choice>
tags that has answer
attr - otherwise don't add append text.
Also, I tested it on IE8 compatible version - works fine.
Upvotes: 1