user2811083
user2811083

Reputation: 83

get image out of CDATA and description tag with jquery

How do i get a image out of a CDATA and description tag? Here is the XML code:

<description>
    <![CDATA[
        <img src='http://www.autowereld.com/imagesDB/100/311151018460_.jpg' border='0'                     bordercolor='black' style='float:left; margin-right: 8px;margin-bottom: 8px;'>
    ]]>

    Het is Subaru wat de klok slaat deze week. Na spyshots van de Subaru Legacy Touring,     gelekte foto’s van de productierijpe WRX en de officiële bekendmaking van de Legacy Concept is     het nu tijd voor de Crossover 7 Concept.
</description>

This is what i got:

var img = $(element).find("description").text();
img = img.replace("<![CDATA[", "").replace("]]>", "");
console.log(img);
$("#img").append('<img src="' + img + '">');

Upvotes: 2

Views: 1775

Answers (2)

Rohan Kumar
Rohan Kumar

Reputation: 40639

You have to escape [ and ] like,

img = img.replace("<!\[CDATA\[", "").replace("\]\]>", "");

Upvotes: 0

Riccardo Canella
Riccardo Canella

Reputation: 200

have you try to:

var img = $(element).find("description").text();
        img = img.replace("<![CDATA[", "").replace("]]>", "");
        console.log(img);
        $("#img").append(img); // <-- use only the var ing

to remove the CDATA and image tag out of the description tag :

var dsr = $(element).find("description").text();
dsr= dsr.substring(dsr.indexOf('>')+1, dsr.length);

Upvotes: 1

Related Questions