Reputation: 1331
I am using jquery ajax to get the names stored in an xml file and append it to a div id, but having some problems.
$(function(){
$.ajax({type: "GET",
url: "names.xml",
dataType: "xml",
cache: false,
success: function(xml){
$(xml).find('name').each(function(){
$(this).text().append('#names');
});
}});
});
my xml file is formatted like so
<?xml version="1.0"?>
<item>
<name>Paul</name>
<name>John</name>
<name>Sam</name>
<name>Mitch</name>
etc.
</item>
and need to be appended to div id name.
<div id="names">
Paul
John
Sam
Mitch
</div>
Help
Upvotes: 2
Views: 7017
Reputation: 360026
Try this:
$(function()
{
var $names = $('#names');
$.ajax(
{
type: "GET",
url: "names.xml",
dataType: "xml",
cache: false,
success: function(xml)
{
var combinedText = $(xml).find('name').map(function()
{
return ' ' + $(this).text();
}).get().join('');
$names.text($names.text() + combinedText);
}});
});
Upvotes: 1
Reputation: 166061
You are using append
the wrong way round - the element to append to needs to come first:
$(xml).find('name').each(function() {
$("#name").append($(this).text());
});
Here is an example fiddle.
Upvotes: 3