I have an HTML string containing text and images like
<p>...</p> <img src="..." /> <p>...</p>
I need to get the src attribute of the first image. An image may or may not appear after <p> , and there may be more than one image or no image at all.
At first I tried adding a line to the DOM and filtering. But, if I do this, the browser requests all external resources. In my situation, this adds a lot of extra overhead.
My initial approach:
var holder = $('<div></div>'); holder.html(content); var src = holder.find('img:first').attr('src');
How can I get src of the first image without adding HTML? Do I need to use regex, or is there a better way?
The solution should be based on javascript / jQuery - I do not use any server languages.
My question is very similar to: http://forum.jquery.com/topic/parsing-html-without-retrieving-external-resources
thanks
javascript jquery attributes
Db.
source share