I am building an HTML/CSS/Javascript application that dynamically embeds facebook posts based on an API that returns a list of facebook post ID's. I loop through the post ID's, generate an embed code with postId inserted for each post, and append to the DOM. Simple stuff.
However I need to append some simple text above each post, and I cannot figure out how to do this dynamically in Javascript.
UPDATE 3.28 I know that I cannot append anything to the iframe generated by the embed code, and that I have to append the text before or after the iframe element as mentioned in comments below. I updated my code below so that the additional text is being appended to the parent container for the posts, as opposed to trying to append to the iframe itself.
This works as far as getting the text to appear on the page, but I'm getting a list of text elements that appear after all the iframe elements, as opposed to one text element for each iframe. See below for screenshots of what is generated:
// Array of facebook post ID's
facebookArr = [{
post_id: "pfbid0cm7x6wS3jCgFK5hdFadprTDMqx1oYr6m1o8CC93AxoE1Z3Fjodpmri7y2Qf1VgURl"
},
{
post_id: "pfbid0azgTbbrM5bTYFEzVAjkVoa4vwc5Fr3Ewt8ej8LVS1hMzPquktzQFFXfUrFedLyTql"
}
];
// Variables to store post ID, embed code, parent container
let postId = "";
let embedCode = "";
let facebookContainer = document.getElementById("facebook-feed-container");
$(facebookContainer).empty();
// Loop through data to display posts
facebookArr.forEach((post) => {
let relativeContainer = document.createElement("div");
postId = post.post_id;
postLink = `${postId}/?utm_source=ig_embed&utm_campaign=loading`;
// ---> UPDATE: separate container element
let iframeContainer = document.createElement("div");
embedCode = `<iframe src="https://www.facebook.com/plugins/post.php?href=https%3A%2F%2Fwww.facebook.com%2FIconicCool%2Fposts%2F${postId}&show_text=true&width=500" width="200" height="389" style="border:none;overflow:hidden" scrolling="no" frameborder="0" allowfullscreen="true" allow="autoplay; clipboard-write; encrypted-media; picture-in-picture; web-share" id=fb-post__${postId}></iframe>`;
// Update the DOM
iframeContainer.innerHTML = embedCode;
// ADDITIONAL TEXT
let additionalText = document.createElement("div");
additionalText.className = "absolute";
additionalText.innerText = "additional text to append";
relativeContainer.append(additionalText, iframeContainer);
facebookContainer.append(relativeContainer);
});
#facebook-feed-container {
display: flex;
flex-direction: row;
row-gap: 1rem;
column-gap: 3rem;
padding: 1rem;
}
.absolute {
position: absolute;
color: red;
}
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<div id="facebook-feed-container"></div>
iframe
elements, and that doesn't make much sense. (Children of iframe elements would only be shown as fallback content, if the browser wasn't capable of handling iframes in the first place.)relative
elements containing an iframe each, and then following those, you got twoabsolute
elements. The absolute elements need to go into the relative containers, not be appended after them. (This would probably be a bit easier, if you did both things in one loop.)