I'm using appendChild
to insert an iframe between paragraphs dynamically.
The problem is that (for some reason) the iframe is being inserted BEFORE the </p>
and not AFTER, as I need.
My original HTML:
<p>Some text</p>
<p>Some text</p>
<p>Some text</p>
<p>Some text</p>
My JavaScript:
var elements = document.getElementsByTagName('p'); // array of paragraphs
var ifrm = document.createElement("iframe"); // create iframe
ifrm.setAttribute("src", "https://example.com"); // set iframe URL
elements[2].appendChild(ifrm); // insert iframe
What I get:
<p>Some text</p>
<p>Some text</p>
<p>Some text <iframe src="..."></iframe> </p>
<p>Some text</p>
What I expect to get:
<p>Some text</p>
<p>Some text</p>
<p>Some text</p> <iframe src="..."></iframe>
<p>Some text</p>
What I'm doing wrong?
, (so it becomes a sibling, not a child)
– V Maharajh Apr 30 '21 at 20:55