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?
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?
The new generation of project management tools

With’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

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.
All Courses

From novice to tech pro — start learning today.