Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get All img tags out of xml file (jquery)

Tags:

jquery

xml

I have the following xml file

<gallery>
<album title="test" description="test" lgPath="/images/commphotos/">
    <img src="1130975173.jpg" />
    <img src="1475634985E.jpg" />
    <img src="1889677107.jpg" />
    <img src="1356256436.jpg" />
    <img src="834682273.jpg" />

</album>
</gallery>

I've been trying to read it using jquery to get out each of the img tags. Whats the best way to do this?

like image 715
Vibration Of Life Avatar asked Dec 06 '25 05:12

Vibration Of Life


1 Answers

With the .map() function, you can easily get all the src attributes of those imgs into a Javascript array. Of course you can use .each() also, but it is much more elegant this way.

var srcArray = $(xml).find('img').map(function () {
    return this.src;
}).get();

jsFiddle Demo

In my example, .get() is used to create a simple array from the jQuery object returned.

like image 141
kapa Avatar answered Dec 09 '25 01:12

kapa