title: Select an html element created with JS in JS
body: <p>I need to create a <code>h1</code> element using JavaScript and then add a content to that <code>h1</code>. This is what I tried:</p>
  
  <pre><code>&lt;div id="time"&gt;
  &lt;/div&gt;
  &lt;script&gt;
    var h1 = document.getElementById("time").createElement("h1");
    h1.id= "timeh1";
    document.getElementById("timeh1").innerHTML = "Good Afternoon!";
  &lt;/script&gt;
  </code></pre>
  
  <p>and </p>
  
  <pre><code>&lt;div id="time&gt;
  &lt;/div&gt;
  &lt;script&gt;
    document.getElementById("time").createElement("h1");
    document.getElementByTagName("h1")[0].setAttribute("id", "time-h1");
    document.getElementById("time-h1").innerHTML = "Good Afternoon!";
  &lt;/script&gt;
  </code></pre>
  
  <p>and</p>
  
  <pre><code>document.getElementById("time").createElement("h1").innerHTML = "Good Afternoon!";
  </code></pre>