tags:

views:

64

answers:

2

I am trying to take the contents of 'description' and place it into a div. Below is the XML that comes from a jQuery ajax call.

<media> 
    <entry> 
        <title>Item Name</title> 
        <description>
            <p>First Line<br />
            Second Line</p>
            <p>More Content</p>
        </description> 
        <author>Name</author> 
        <date>2010-07-06</date> 
    </entry> 
</media> 

I have tried the following, but cannot get it to works. At best it shows up, but without any formatting (FF3). Otherwise a 'WRONG_DOCUMENT_ERR' error is shown (Chrome).

$.ajax({
    url: xml_url,
    dataType: "xml",
    success: loadItem
});

function loadItem(data) {
    $(data).find('entry').each(function() {
        $(".playing div.description").html($(this).find("description"));
    });
}

Is it even possible? Thanks.

A: 

Im not entirely certain but everytime I have done this in the past it looks more like this:

function loadItem(data) {
    $(data.responseXML).find('entry').each(function() {
        var descr = $(this).find('description').val();
        $(".playing div.description").html(descr);
    });
}

The key change there (I think) is .responseXML and using the value of description rather than the XML node.

HurnsMobile
Thanks for the help. But data.responseXML returns back undefined.
Nathanael
Tried again. Found out that .responseXML is part of the XMLHttpRequest (http://api.jquery.com/jQuery.ajax/). But I get the same problem - no formatting (FF3) and a 'WRONG_DOCUMENT_ERR' error is shown (Chrome).
Nathanael
Have you tried alerting out the value of `descr` or in your code `$(this).find("description")` ? Does the firebug console give you any further errors/info?
HurnsMobile
This is not the correct answer, as none of the XML in the question involves attributes on any of the elements. Thus, the `.attr()` call in this answer is not going to fix anything.
Pointy
Sorry about that - I blame a lack of afternoon coffee...
HurnsMobile
@HurnsMobile There are no errors in firebug. When I alert the descr it says it is an Object. And it works, the content is displayed in FF, but the p tags are not 'working' nor is the br tag. Chrome gives an error.
Nathanael
@HurnsMobile No problem, I simply changed it to .find("description"), instead of .attrThanks for the help.
Nathanael
@Nathanael Aha! So instead of the string you are getting the object back. Im not sure which but either try `.find("description").html()` or `.find("description").val()` and I think you will have it fixed.
HurnsMobile
.val() returns an empty string, and .html() gives an error.Don't worry, I will use the .load() function instead (with a HTML output), as it is working.Thanks.
Nathanael
+2  A: 

Hey, I think I worked out a solution (I have been working on this for hours with no avail, so asked the question, and then I manage to work it out soon after, typical, lol).

Simply I used load, and it carries all the formating across.

$('.playing div.description').load(xml_url + ' description');
Nathanael