title: Write content to iframe within an iframe javascript/jquery
body: <p>I think what I've got might not require much adjusting, but when I try the following:</p>
  
  <pre><code>&lt;div id="exp_div"&gt;&lt;/div&gt;
  &lt;script&gt;
  procedure = [
      [
        "content 1","content 2"
        ]
      ]
  
  procedure.forEach(function(this_proc,index){
    $("#exp_div").append("&lt;iframe id='trial"+index+"' width='800px' height='800px'&gt;&lt;/iframe&gt;");
    trial_iframe_code = '';
    for(var i = 0; i &lt; this_proc.length; i++){
       trial_iframe_code += "&lt;iframe id='post"+i+"'&gt;&lt;/iframe&gt;";
    }
    doc = document.getElementById('trial'+index).contentWindow.document;
    doc.open();
    doc.write(trial_iframe_code);
    doc.close();
    for(var i = 0; i &lt; this_proc.length; i++){
      trial_content = this_proc[i];
        console.dir(trial_content);
      doc = document.getElementById('trial'+index).contentWindow.document.getElementById('post'+i);
      doc.open();
      doc.write(trial_content);
      doc.close();
    }
  })
  &lt;/script&gt;
  </code></pre>
  
  <p>i.e. at this <a href="https://jsfiddle.net/vj78s86b/" rel="nofollow noreferrer">fiddle</a> (iframes don't work as well when i try to run the code on SO) I get the error that "doc.open is not a function" - which I think relates to the attempt to write on the "post" iframe within the trial iframe. Is it possible to write to an iframe within an iframe in this sort of structure? Is my code close to correct, or is a different approach required? </p>
  
  <p>To clarify, this is for an online psychology experiment to create all the tasks the participant will do in a series of iframes. As some of the tasks will have multiple tasks within them, I want to write multiple iframes within an iframe when this happens.</p>
  
  <p>Working code in this case would produce an iframe called "trial0" within "exp_div". Within "trial0" there would be an iframe with id "post0" which just has "content 1" written in it, and an iframe with id "post1" which has "content 2" written in it.</p>