Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find <img> tag inside text string

I have an xml document (from a feed), which I'm extracting values from:

$(feed_data).find("item").each(function() {
    if(count < 3) {
    //Pull attributes out of the current item. $(this) is the current item.
        var title = $(this).find("title").text();
        var link = $(this).find("link").text();
        var description = $(this).find("description").text();

Now inside "description" i need to get the img element, but this is causing me some problems. "descripttion" is a regular string element and it seems i can't call the ".find()" method on this, so what do i do?

I have tried calling .find():

var img = $(this).find("description").find("img");

But it's a no go. The img is wrapped in a span, but I can't get to this either. Any suggestions? I'd prefer to avoid substrings and regex solutions, but I'm at a loss.

I've also tried turning the "description" string into an xml object like so:

var parser = new DOMParser();
var desc = parser.parseFromString(test,'text/xml'); 

$(desc).find("img").each(function() {
    alert("something here");
});

But that doesn't work either. It seems like it would, but I get a "document not well formed" error.

like image 226
mac Avatar asked Mar 19 '26 19:03

mac


1 Answers

Try enclosing the contents of the description tag in a dummy div, that seemed to work better for me, and allowed jQuery's .find() to work as expected.

e.g.

$(feed_data).find("item").each(function() {
    if(count < 3) {
        //Pull attributes out of the current item. $(this) is the current item.
        var title = $(this).find("title").text();
        var link = $(this).find("link").text();
        var description = '<div>' + $(this).find("description").text() + '</div>';
        var image = $(description).find('img');
like image 81
GregL Avatar answered Mar 21 '26 09:03

GregL