GTS Joe GTS Joe - 2 months ago 8
HTML Question

How to find element in XML Data Island using jquery?

How can I use jQuery to output an XML data island? Here is an example of my code:

<script id="XML" type="application/xml">
<designs>
<design id="17161" style="44DSW" image="/500.jpg"></design>
</designs>
</script>


console.log( jQuery('designs design[id="17161"]').attr('style') );


The output should be:
44DSW

Answer

You need to use jQuery.parseXML() to parse string into XML document. Parsed xml is like HTML document and you can find your element in it.

var xml = $.parseXML($('#XML').text());
var style = $(xml).find('design[id="17161"]').attr('style');
console.log(style);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script id="XML" type="application/xml">
  <designs>
    <design id="17161" style="44DSW" image="/500.jpg"></design>
  </designs>
</script>