Answers for "get an html img tag from a string"

1

get an html img tag from a string

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") : "";
Posted by: Guest on October-13-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language