最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to print text from textarea? - Stack Overflow

programmeradmin5浏览0评论

I want to print text from text area.

I have a textarea which text can be updated by user. When user update text from textarea and then print the updated text can be print on page. And this text can be print on print page without textarea.

Please suggest any solution.

Thanks

I want to print text from text area.

I have a textarea which text can be updated by user. When user update text from textarea and then print the updated text can be print on page. And this text can be print on print page without textarea.

Please suggest any solution.

Thanks

Share Improve this question edited Jan 19, 2011 at 10:13 intellidiot 11.2k4 gold badges37 silver badges41 bronze badges asked Jan 19, 2011 at 7:33 JayashriJayashri 4,3336 gold badges26 silver badges18 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 21

I think I got what you are asking for. Give it a try:

<html>
  <head>
    <title>Print TextArea</title>
    <script type="text/javascript">
      function printTextArea() {
        childWindow = window.open('','childWindow','location=yes, menubar=yes, toolbar=yes');
        childWindow.document.open();
        childWindow.document.write('<html><head></head><body>');
        childWindow.document.write(document.getElementById('targetTextArea').value.replace(/\n/gi,'<br>'));
        childWindow.document.write('</body></html>');
        childWindow.print();
        childWindow.document.close();
        childWindow.close();
      }
    </script>
  </head>
  <body>
    <textarea rows="20" cols="50" id="targetTextArea">
      TextArea value...
    </textarea>
    <input type="button" onclick="printTextArea()" value="Print Text"/>
  </body>
</html>

Basically this will open another child window and execute javascript print on that so that the textarea and other things don't get printed.

发布评论

评论列表(0)

  1. 暂无评论