HTML: How to Retain Formatting in Textarea

HTML : How to retain formatting in textarea?

What you want is a Rich Text Editor. The standard HTML <textarea> tag only accepts plain text (even if the text is or includes HTML markup). There are a lot of example out there (including some listed on the page linked) but I would highly recommend using a prepackaged one for this. Coding your own is fairly complicated for people who are new, and even for a lot who have some experience. Both TinyMCE and CKEditor are very common ones, but there are many others as well.

Preserve line breaks in textarea

Generally you just need to add

  • white-space: pre-line; whitespace trimmed to single whitespace or

  • white-space: pre-wrap; all whitespace preserved

to the element's style (CSS), where you want your text rendered with line-breaks.

Can I embed HTML formatting inside of a textarea tag?

I am pretty sure the answer is no -- cannot do it with a textarea.

From the MDN docs:

The HTML <textarea> element represents a multi-line plain-text editing control.

  • Permitted content Text

How to send text area data with formatting to a database and retrieve it the same way

You can take the user entered list (or any rich-text) store it in mongodb as HTML.

Check this article for more details about how to do it: Link

This should give you the flexibility to add meta-information about making the text bold, italic etc.



Related Topics



Leave a reply



Submit