3

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

3
  • Is it a string or an actual element? Commented Jun 30, 2013 at 8:02
  • Are you interested in jQuery solutions? Commented Jun 30, 2013 at 8:02
  • Thanks, I retrieve the string by: str1=document.getElementById("images").innerHTML but I'm not very good at javascript! Commented Jun 30, 2013 at 9:05

2 Answers 2

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

for (var i=0; i<imgs.length; i++) {
    if (imgs[i]["style"]["display"] === "inline") {
        console.log(imgs[i]["src"]);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

why not use the shorter object syntax? imgs[i].style.display
0

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.

3 Comments

Note that he is looking for the src of the display: inline img.
@AustinBrunkhorst have now put it in a function. Happy?
Did you even read his question? He doesn't know the ID of the image with the display inline property. Making it a function doesn't make a difference from what you originally had.

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.