Search code examples
javascriptjqueryhtmltextrtf

How to generate HTML tags by copy/paste into textarea


When pasting a formatted text copied from MS Word into textarea, it will paste plain text by ignoring all markups. I am looking for a JavaScript (and preferedly jQuery) method to convert some formatting markups to HTML tags.

For example, pasting <b>Text which was bold in MS Word</b>


Solution

  • Catching the paste event isn't easy and hacky at best (see JavaScript get clipboard data on paste event (Cross browser)), so I think your best bet for achieving this is to attach some JS to your textarea with its own 'paste rich text' method, which will pull from the clipboard and format to HTML for you.

    There are loads of WYSIWYG editors out there that would do this, but I quite like CKEditor. You can disable all functionality apart form 'rich text paste' to give a minimalist toolbar.