Blogspot post content : How to get second image src using javascript

437 Views Asked by At

I'm working on a blogspot template and I need to get the second image from post content

I've tried this code but I didn't get any results:

var postcontent = entry.content.$t; // The Post Content 
var images = postcontent.getElementsByTagName('img'); 
 document.write(images[1]);
2

There are 2 best solutions below

0
vishnu prasath On

I hope this fits your use case, also if this dosent work log the item object to see how you can access the data

var postcontent = entry.content.$t; // The Post Content 
var images = postcontent.getElementsByTagName('img'); 
images.forEach(item=>{
 console.log("The item is",item)
 document.write(item.src)
 })

;
0
AudioBubble On

entry.content.$t contains a javascript string.

So you need to append the string to an element before using getElementsByTagName

var postcontent = entry.content.$t; // The Post Content 
var elem = document.createElement('div');
elem.innerHTML = postcontent;
var images = elem.getElementsByTagName('img');
document.write(images[1]);