JQuery can't return xml value

Posted by Mistergreen on Stack Overflow See other posts from Stack Overflow or by Mistergreen
Published on 2012-12-11T17:02:30Z Indexed on 2012/12/11 17:03 UTC
Read the original article Hit count: 230

Filed under:
|

I have this simple xml

<?xml version="1.0"?>
<library>
    <item name="box_shelf">
        <imageback src="images/box_shelf_color.png"/>
        <outline src="images/box_shelf_outline.png"/>
        <sku>
            <wh sku="4696424171" />
            <ch sku="4696424179" />
            <choc sku="4696425863" />
            <ma sku="4696424175" />
        </sku>
    </item>
</library>

Loading the xml is fine. I then have a function to parse certain nodes.

function parseImageXml(mainNode,targetNode) {
            $(libraryXML).find('item').each(function() {

                if($(this).attr('name') == mainNode) {
                     var $temp = $(this).find(targetNode).attr('src');
                     console.log("******"+$temp);
                    return $temp;
                }

            });
}

The console log is fine with

******images/box_shelf_outline.png
******images/box_shelf_color.png

But when I try to return the value into variables, I get undefined.

var image_outline = ''+parseImageXml("box_shelf","outline");
var image_back = parseImageXml("box_shelf","imageback");
console.log(image_outline+":"+image_back);

undefined:undefined

Any insight would be great, thanks

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about Xml