Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9042239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T10:26:12+00:00 2026-06-16T10:26:12+00:00

I am trying to use javascript to print out a text field from a

  • 0

I am trying to use javascript to print out a text field from a dbase where the textfield is served by php to a textarea that has an id. The print javascript function takes the content from the textarea using getElementById and prints it. But here is the issue. To display properly in the textarea, the textfield has newlines in it. These are invisible in the textarea. They just show up as line breaks. (They are also invisible using PHP admin in the dbase tables. They show up there as linebreaks as well).

However, to get the linebreaks to show up when you print the text, they need to be converted into <br>. Otherwie, you see and print a runown sentence.

I think I need to search for something in the javascript variable, perhaps \n and replace with
However, I can’t seem to get linebreaks to display in printed page (wich is html based.)

Can anyone suggest right way to do this? Thanks.

Flawed code:
js
f

unction printit(id) {
var toprint = document.getElementById(id).innerHTML;
toprint = toprint.replace("\n","<br>");
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(toprint); 
    win.document.write('<'+'/'+'body'+'><'+'/'+'html'+'>');
    win.document.close();
    win.print();
    win.close();
  }

html

<textarea id="textarea">A whole bunch
of text
with line breaks
goes here
</textarea><a href="javascript:void(0);" onclick="printit('textarea');">Print text</a>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T10:26:13+00:00Added an answer on June 16, 2026 at 10:26 am

    You’re probably only replacing the first line break. Try the global flag to replace them all:

    toprint = toprint.replace(/\n/g, '<br />');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im trying to use javascript in my textarea to display text until the user
I'm trying to use Javascript to access data from http://www.bart.gov/dev/eta/bart_eta.xml , however, I found
So I'm new to Js & php and I'm trying to print out a
I have some javascript that i'm trying to use in a jsf app. I've
I am simply trying to use jQuery to print out the contents of an
I am trying to execute a javascript function from a PHP script by sending
I'm trying to use CSS ( under @media print ) and JavaScript to print
I'm trying to use Javascript's built-in replace() method, but for some reason it won't
I am trying to use JavaScript to start a marquee when a user puts
I am trying to use JavaScript to set an elements width relative to their

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.