0

I want to create a div inside an iframe, call the divID and change it. Can it be done from the head using JS?

Here is what I tried and won't work for me:

console.log(document.getElementById("mydiv").id);
document.getElementById("myDiv").setAttribute("id", "MyDiv2");
console.log(document.getElementById("MyDiv2").id);
<body>
  <div id="mydiv" style="border: solid 2px #000;">
    <iframe height="100px" width="100%" name="iframe_a"></iframe>
    <div>
      <h2>This is mydiv</h2>
    </div>
  </div>
</body>
Daniel
  • 1
  • 1
  • Same Origin Policy. If the iframe is on your domain, yes, it's possible. Otherwise, imagine you buy a domain like evilbank.com, and inside you iframe fullscreen bank.com and you go edit content at will. That would be evil, right? If the other domain trusts you it's also possible. But in that case they need to embed a certain amount of code into their page in order to allow the Broadcast Channel messaging API to communicate with your page - and expose objects etc. – Roko C. Buljan Apr 02 '21 at 10:07
  • Where is the code that creates the div, where is the code that adds the div? – mplungjan Apr 02 '21 at 10:09

0 Answers0