Masood Hosseini
Masood Hosseini

Reputation: 193

Get string attribute of a tag in Javascript

I need some help to extract the inline image source (in this case #3) from the string below in javascript, then encapsulate and send it to php:

<img width="100" id="1" style="display: none;" src="http://col.stb00.s-msn.com/i/4E/5EA45CFEC5FF5726D86E65CEE815D.jpg">
<img width="100" id="2" style="display: none;" src="http://col.stb01.s-msn.com/i/36/59F78F98816E925C8A18FBCF013D5.jpg">
<img width="100" id="3" style="display: inline;" src="http://col.stb00.s-msn.com/i/6F/D11A5421FDC5E8C5CEA4D19BCC7A5.jpg">
<img width="100" id="4" style="display: none;" src="http://col.stb00.s-msn.com/i/88/B51A1462A325FF345AC442688F7A8.jpg">
<img width="100" id="5" style="display: none;" src="http://col.stb01.s-msn.com/i/39/8A811756CB49259F65032AB9F1D78.jpg">

Is there an easy way to do it please? thanks

Upvotes: 3

Views: 108

Answers (2)

imulsion
imulsion

Reputation: 9040

Easy. Use DOM:

function getSrc(id)
{
    var src = document.getElementById(id).src;
}

The variable src will hold the value of the src attribute in your tag. Do this for all of the tags that need their src elements extracted. You will probably need to use AJAX to pass this to PHP.

Upvotes: 0

Kabie
Kabie

Reputation: 10663

var imgs = document.getElementsByTagName('img');

for (var i=0; i<imgs.length; i++) {
    if (imgs[i]["style"]["display"] === "inline") {
        console.log(imgs[i]["src"]);
    }
}

Upvotes: 1

Related Questions