title: document.createElement / document.body.appendChild not creating / writing div where executed
body: <p>I have a div with the ID 'headercontent' and I have a script that will write a link amongst the others if javascript is enabled on the users system, but also has a backup using noscript just in case the user does not have javascript enabled.</p>
  
  <p>The code runs fine and the 'a' element is written when executed but the problem is that the code is not written in the div it is executed in. It writes it outside of the 'headercontent' div and it ignores the class style completely, even though it is written in the rendered code. I'm not too worried about the styling/class because I can just add a style attribute to the element and get it written by javascript if necessary, but I'm more concerned about why its writing the code outside of this div.</p>
  
  <p>My code is:</p>
  
  <pre><code>&lt;div id="headercontent"&gt;
      hey, this is a div.
      &lt;a href="#" class="button"&gt;This is a link&lt;/a&gt;
      &lt;a href="#" class="button"&gt;This is another link&lt;/a&gt;
  
      &lt;script type="text/javascript"&gt;
          writeask()
          function writeask() {
           var writeaskbox = document.createElement('a');
           writeaskbox.href = 'javascript:askbox()';
           writeaskbox.className = 'button';
           writeaskbox.innerHTML = 
               ['Ask'].join(' ')
           document.body.appendChild(writeaskbox);
          }
  
          function askbox(){
           var askbox = document.getElementById('askbox')
           if (askbox.style.display == 'none') {
            askbox.style.display = 'block'
            askbox.style.height = '150px'             
           } else {
            askbox.style.display = 'none'
            askbox.style.height = '0px'
           }
          }
      &lt;/script&gt;
      &lt;noscript&gt;&lt;a href="/ask" class="button"&gt;Ask&lt;/a&gt;&lt;/noscript&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>How do I get the writeask() function to create this a element in the same div it is executed in? So that the final output is:</p>
  
  <pre><code>&lt;div id="headercontent"&gt;
      hey, this is a div.
      &lt;a href="#" class="button"&gt;This is a link&lt;/a&gt;
      &lt;a href="#" class="button"&gt;This is another link&lt;/a&gt;
      &lt;a href="javascript: askbox()" class="button"&gt;Ask&lt;/a&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Instead of:</p>
  
  <pre><code>&lt;div id="headercontent"&gt;
      hey, this is a div.
      &lt;a href="#" class="button"&gt;This is a link&lt;/a&gt;
      &lt;a href="#" class="button"&gt;This is another link&lt;/a&gt;
  &lt;/div&gt;
  &lt;a href="javascript: askbox()" class="button"&gt;Ask&lt;/a&gt;
  </code></pre>
  
  <p>I'm still a beginner with javascript so I'm rather puzzled now. If anyone could help, that would be well appreciated.</p>
  
  <p>Thank you in advance. Dan.</p>