0

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:

What appears on the page Resulting code in inspector

// 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>

11
  • 1
    You can not append anything into the iframe - it is loaded from a different origin, so you have no JavaScript access to its contents at all. You could only add stuff to your own document, before or after the iframe element. If you need it to partially overlap the iframe content(?), then position it absolute.
    – CBroe
    Commented Mar 27 at 10:31
  • 1
    Are you talking about the code as currently shown above, or did you modify it? Because in what's currently showing here, you are trying to append the additional text to the 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.)
    – CBroe
    Commented Mar 28 at 8:31
  • 1
    Put each iframe and the additional text into a separate container element - and position that container relative, so that it becomes the reference point for absolute positioned children.
    – CBroe
    Commented Mar 28 at 9:13
  • 1
    Check the resulting DOM, using your browser dev tools - you currently created two relative elements containing an iframe each, and then following those, you got two absolute 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.)
    – CBroe
    Commented Mar 28 at 9:52
  • 1
    Structure is still wrong. Now you have each "absolute", and each iframe, in separate wrapper divs.
    – CBroe
    Commented Mar 28 at 10:54

0

Browse other questions tagged or ask your own question.