Richard Lloyd
Richard Lloyd

Reputation: 21

getElementById doesn't get textarea content

This might seem simple for someone that knows: This script gets a DIV tag when the button is clicked, and opens a new page with the contents. But I want it to show the contents of the textarea after the user has typed something in.

Any ideas? code is:

<script type="text/javascript">
  var win=null;
  function printIt(printThis)
  {
    win = window.open();
    self.focus();
    win.document.open();
    win.document.write('<'+'html'+'><'+'head'+'><'+'style'+'>');
    win.document.write('body, td { font-family: Verdana; font-size: 10pt;}');
    win.document.write('<'+'/'+'style'+'><'+'/'+'head'+'><'+'body'+'>');
    win.document.write(printThis.innerHTML);
    win.document.write('<'+'/'+'body'+'><'+'/'+'html'+'>');
    win.document.close();
    win.print();
  }
</script>
</head><body>
    
  <div id="activity5">
    <blockquote>
      <table border="0" cellpadding="3">
      </table>
      <form id="form4" name="form1" method="post" action="">
      <textarea name="Activity1a4" cols="80" rows="15" class="s23" id="ta1"></textarea>
      <p>
        <input name="print4" type="submit" id="print4" value="Print" onclick="printIt(document.getElementById('activity5')); return false"/>
          * Retain this learning activity as part of your portfolio of evidence.</p>
      <p>Check your findings with the correct answer in the back of this Learner Resource under 'Learning activity answers'.</p>
      </form>
    </blockquote>
  </div>
</body>

Upvotes: 2

Views: 4539

Answers (2)

jfriend00
jfriend00

Reputation: 707696

You get the contents of a textarea with .value, not with .innerHTML.

That means that getting activity5 innerHTML will NOT include the text that has been typed in the child textarea. If you want to include that text, you will have to separately retrieve that text with .value on the textarea and insert it into the new text area in the new page.

Upvotes: 2

x1a4
x1a4

Reputation: 19485

You'll need to get the contents of the textarea separately with the value attribute, then set it explicitly the same way. Take a look at http://jsfiddle.net/EPvWV/ for an example of how to do that.

Upvotes: 2

Related Questions