0

I have been trying to get the 'src' in img tags inside a javascript variable that contains a block of HTML code dynamically generated/assigned.

Eg:

var post_body = "<div>This is an image <img src='abcd.jpg' /> and this is a paragraph <p>hi there</p> Here we have another image <img src='pqrs.jpg' /></div>"

the content of the 'post' variable is not predefined and it is dynamic and the HTML code in it always changes. To get the src of the image in it, I did the following. But it does not always work for me.

var firstimg = $(post_body).find("img:first").attr("src");

I was trying to get the first image of a blog post from the blog post's content but this does not work for some posts having images. How can this be done using javascript or jQuery without failing?

4
  • 2
    You are referring to post_body while your variable is called post. Commented Oct 6, 2013 at 10:10
  • Sorry I have changed the question's text accordingly. 'post' must be changed as 'post_body'. That is why I have used post_body thereafter by a mistake while preparing this question. Commented Oct 6, 2013 at 12:34
  • 1
    It works here: jsfiddle.net/3SLJm. What is the scenario where it fails? Commented Oct 6, 2013 at 13:43
  • Have you found a solution for this? Commented Oct 28, 2015 at 9:54

4 Answers 4

8

Using plain JavaScript, dump the HTML into a temporary element and extract it:

var div = document.createElement('div');
div.innerHTML = post_body;
var firstImage = div.getElementsByTagName('img')[0]
var imgSrc = firstImage ? firstImage.src : "";
// or, if you want the unresolved src, as it appears in the original HTML:
var rawImgSrc = firstImage ? firstImage.getAttribute("src") : "";
Sign up to request clarification or add additional context in comments.

3 Comments

We cant say the string starts with a div or a p tag. It is totally unpredictable and cannot use this answer.
Um... why does it matter what the string starts with? This code works with any HTML...
This works but forces the browser to download all of the photos linked in the temporary element.
2

Change $(post_body) to $(post)

Try

var post = "<div>This is an image <img src='abcd.jpg' /> and this is a paragraph <p>hi there</p> Here we have another image <img src='pqrs.jpg' /></div>";
var firstimg = $(post).find('img:first').attr('src');

2 Comments

Sorry I have changed the question's text accordingly. 'post' must be changed as 'post_body'. That is why I have used post_body thereafter by a mistake while preparing this question.
This does not work at times due to quotes in the html string so that $(post) gives an error. Needs another way.
0

Perhaps you can try adding id to img tag and then access it.

var oImg = document.getElementById('myImg');
var attr = oImg.getAttribute('src');

This is a pure js solution.

1 Comment

image tags cannot be predefined, they are automatically loaded from an online source.
0

Late but, if don't want the images to be loaded use the below.

var div = document.createElement('template');
div.innerHTML = post_body;

if you create div element the remote images are loaded the moment you insert innerHTML, template prevents the requests.

Comments

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.