2

I was trying to append an Iframe embed to a div element using javascript but I'm getting an error when trying to append it using innerHtml. Is there any way to append an Iframe in a div using javascript. My current code is attached below. Thanks in advance.

function addCode() {
  document.getElementById("add_to_me").innerHTML +=
    "<iframe width="
  1263 " height="
  480 " src="
  https: //www.youtube.com/embed/zogtwLQQEfY" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>";
}
<div id="add_to_me">
  <h3> Heading</h3>
  <!--embed Iframe here-->
</div>

1 Answer 1

2

The string contains attributes that are wrapped with double quotes - ". Wrap the entire string with single quotes - '.

document.getElementById("add_to_me").innerHTML +=
  '<iframe width="1263" height="480" src="https://www.youtube.com/embed/zogtwLQQEfY" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>';
<div id="add_to_me">
  <h3> Heading</h3>
  <!--embed Iframe here-->
</div>

Sign up to request clarification or add additional context in comments.

4 Comments

is there any way to save that appended code to the default HTML permanently @Ori Drori
You'll need to change the original HTML file to do so. The other option is to change via JS if you can add it to the page.
By change the original HTML You mean recreating a new file with the updated code in it? If so does this mean there is no way of updating the default code automatically when the addcode function runs?@Ori Drori
Sorry. I don't understand what you need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.