1

I have the following XML file

<PRODUCT>
      <IMAGE><image href="myimage.jpg"/></IMAGE>
      <NAME>TV 47" LCD Full HD Scarlet</NAME>
      <PRICE>4999</PRICE>
</PRODUCT>

As you can see, my element has a href. I can easily get the text data from NAME and PRICE. Now I need to get the href attribute from and load it. This is my script

<script type="text/javascript">
$(function() 
{ 
$.ajax(
{
    type: "GET",
    url: "xml.xml",
    dataType: "xml",
    success: function(xml) 
    {
        $(xml).find('PRODUCT').each(function() 
        {
            var name = $(this).find('NAME').text();
            var price = $(this).find('PRICE').text();
            var image = $(this).find('IMAGE').text();
            $('<p></p>').html(name+'<br />'+price+'<br />'+image).appendTo('#wrap');
        });
    }
});
});
</script>

How do I extract the href attribute and load the corresponding image into a div "wrap"? I tried this Implementing Images in HTML using XML but doesn't work. thank you guys

1
  • you should replace your every .find() function from .children() Commented Jun 7, 2013 at 14:27

3 Answers 3

1
var image = $(this).find('IMAGE').children().attr("href");

To create Object

imgobject=$("<img />").attr("src",image);
Sign up to request clarification or add additional context in comments.

3 Comments

should I create an Image object from the var ?
my div displays [object object] in the place of the image, do you now what is happening?
it depends how are you inserting the image in the div .Tell me the syntax you are using to insert
0

You can try:

var imgUrl = $(xml).find("image[href]").attr("href"); 
$yourdiv.append($("<img src=\"" + imgUrl + "\"/>"));   

Hope it's helpful.

Comments

0

Get the href attribute this way

var image = $(xml).find("image[href]").attr("href"); 

Load the content to the div this way

$('#wrap').append($("<img src=\"" +image+"\"/>"));   

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.