Reputation: 796
I have this function where i want to parse the xml with jquery.
function xmlParser(xml) {
var xmlTitle = xml.data;
alert($(xmlTitle).find('title'));
}
I want to alert the title of the the xml like this but it doesn't work. This is what i did, which can be helpful you to see the xml object.
alert(xml);
alert(xml.data);
xml alerts: [object Object.]
xml.data alerts: <data><title>Hello</title><topic>World</topic></data>
Upvotes: 1
Views: 211
Reputation: 318332
That's an element with the tag title
, which means it's an object, and alerts can't show objects, you should use console.log for that. To show the elements text, you could do:
function xmlParser(xml) {
var xmlTitle = xml.find('title');
alert($(xmlTitle).text());
}
If your getting the XML whith a jQuery method that uses $.ajax
, it should be parsed already, otherwise a good practice is to parse the XML with $.parseXML to make sure jQuery can treat it the usual way.
function xmlParser(xml) {
var xmlTitle = $.parseXML(xml.data);
alert($(xmlTitle).find('title').text());
}
var xml = {}
xml.data = '<data><title>Hello</title><topic>World</topic></data>';
xmlParser(xml)
Upvotes: 10