Based on an answer to this question, I am trying to append elements directly into an empty iframe
with no src
attribute.
However, it seems appendChild
on the iframe
fails silently.
In the code below, doc.write("Hello World");
works correctly, but myform2.appendChild(input2);
does not change the innerHTML
of the frame, and also does not throw an error.
<html>
<body>
<iframe name = "myframe" id = "myiframe">
</iframe>
<script>
var myform2 = document.createElement("form");
var input2 = document.createElement("input");
input2.name = "quantity";
input2.value = "10";
myform2.appendChild(input2);
</script>
<script>
var getFrame = document.getElementById('myiframe');
var doc = getFrame.contentDocument || getFrame.contentWindow.document;
// doc.write("Hello World");
doc.body.appendChild(myform2); // Does not work.
</script>
</body>
</html>
What is the correct way to add a form into the iframe
using raw Javascript?
I am looking for non-jQuery solutions, because I want to understand how the magic works, rather than just letting jQuery perform the magic.