Convert line breaks from plain text into HTML formatted paragraphs


How can I convert line breaks from plain text into HTML formatted paragraphs?

For example, if I have a text area field containing plain text with several paragraphs (or line breaks), how can I enclose each paragraph with <p></p> tags?

Then the resulting HTML formatted text would output to another text area field.

Who is Participating?

Improve company productivity with a Business Account.Sign Up

bugadaConnect With a Mentor Commented:

var text = document.getElementById("textareaid").value;
text = text.split("\n").join("<p>"); 

Open in new window

HonorGodConnect With a Mentor Commented:
use the <pre> tag

for example

Now is the time
for all good men
to come to the
aid of their country.

Is not reformated.  The end of lines are preserved
Does that make sense?

Or, are you asking to convert the user input from a text, or textarea into HTML?

In which case, you want the javascript to surround the value with the <pre> tag for you.

Which is it?
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Can you provide a more complete example?
alien109Connect With a Mentor Commented:
var text = inputTextField.value;
formattedString = text.replace(/^/mg, "<p>").replace(/$/mg, "</p>");
outputTextfield.value = formattedString;
techzzzAuthor Commented:
Yes, I want to convert user input from a textarea field, and then output the new code to another textarea field.
CWS (haripriya)Commented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.