Yusaf Khaliq
Yusaf Khaliq

Reputation: 3393

jQuery .find("img").attr("src") not returning image source

I am using the following jQuery to get the <title> and <content> of an xml feed, for the var contentimg = $(this).find('content').text().find("img").attr("src") there is nothing being returned however if i am to change the variable to $(this).find('content').text(); i am returned the entire content when i am only looking for the content's images src atrribute value.

http://jsfiddle.net/bpBtC/3/

$(document).ready(function () {
    $.ajax({
        type: "GET",
        url: "http://www.blogger.com/feeds/2399953/posts/default",
        dataType: "xml",
        success: xmlParser,
        dataType: 'jsonp'
    });
});

function xmlParser(xml) { 
    $(xml).find("entry").each(function () {
        var title = $(this).find('title').text();
        var contentimg = $(this).find('content').text().find("img").attr("src");
        $(".entirecont").append('<br/>'+title+'<br/>'+contentimg+'<br/><br/><br/>');
    });
}
​

Upvotes: 0

Views: 1633

Answers (2)

Jonny Burger
Jonny Burger

Reputation: 921

You have several errors in your code:

  1. You are not passing the xml to the callback function.
  2. You have to parse the xml first (with $.parseXML)
  3. You have to pass a key and a value parameter to the each function.

Here is a working example (without images from the feed, but that should be self-explaining)

    $(document).ready(function () {
    $.ajax({
        type: "GET",
        url: "http://www.blogger.com/feeds/2399953/posts/default",
        dataType: "xml",
        success: function(xml) {
        xmlParser(xml)
        },
        dataType: 'jsonp'
    });
});

function xmlParser(xml) { 
    parsedxml = $.parseXML(xml)
xml = $(parsedxml)
        entries = xml.find("entry").each(function(key, value) {
        title = $(value).find("title").text()
        $("body").append(title)
        $("<br>").appendTo("body")
        })
}​

Upvotes: 0

smoak
smoak

Reputation: 15034

The content in the <content> tag is encoded. You need to unescape it:

var contentimg = $(unescape($(this).find('content').text())).find("img").attr("src");

Upvotes: 1

Related Questions