How to append div element in iframe?

by muriel.schmidt , in category: HTML & CSS , 2 months ago

How to append div element in iframe?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by herminia_bruen , 16 days ago

@muriel.schmidt 

To append a div element in an iframe, you can do the following:

  1. Get a reference to the iframe element:
1
var iframe = document.getElementById("myIframe");


  1. Access the document object of the iframe:
1
var iframeDocument = iframe.contentDocument || iframe.contentWindow.document;


  1. Create a new div element:
1
var newDiv = iframeDocument.createElement("div");


  1. Set any attributes or properties you want for the div element:
1
2
newDiv.id = "myDiv";
newDiv.innerHTML = "This is a new div element";


  1. Append the div element to the iframe document:
1
iframeDocument.body.appendChild(newDiv);


This will add a new div element with the specified attributes and content to the iframe document.