Reputation: 367
Got a problem returning the text from an xml node. It all needs to be dynamic. Here is the xml:
<instructions>Some ins text.</instructions>
<options>
<option>1.png</option>
<option>2.png</option>
<option>3.png</option>
<option>4.png</option>
</options>
<noOfOptions>4</noOfOptions>
Here is the jquery parse code:
currentPageData.push({
title:$(xml).find("page").attr("name"),
noOfOptions:$(xml).find("noOfOptions").text(),
text:$(xml).find("text").text(),
instructions:$(xml).find("instructions").text(),
option:$(xml).find("option").each(function() {
$(this).text();
}),
The problem is the option section. This just returns one object. I think this is because of the .each function. But I need all of them, that are present to be returned and i need it to return the text in a for loop like this:
for(i=0;i<noOfOptions;i++) {
currentPageData[0].option[0];
}
The above is not working. How can I fix it?
Thanks!
Upvotes: 2
Views: 2174
Reputation: 293
To get the text content of your option nodes try this....
Firstly, your xml isn't valid. I've added a root node
var xml = "<root>
<instructions>Some ins text.</instructions>
<options>
<option>1.png</option>
<option>2.png</option>
<option>3.png</option>
<option>4.png</option>
</options>
<noOfOptions>4</noOfOptions>
</root>",
xmlDoc = $.parseXML( xml ),
$xml = $( xmlDoc ),
$options = $xml.find("option"); // get all option nodes
Then to get each option value using jquery .each
$.each($options, function() {
console.log($(this).text());
});
Hope that helps
Fiddle: http://jsfiddle.net/JohnMcNulty/vRf9Z/
Upvotes: 3