4

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>

Googlebot
  • 13,096
  • 38
  • 113
  • 210

2 Answers2

3

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.

Community
  • 1
  • 1
Karl Barker
  • 10,119
  • 3
  • 18
  • 24
  • Thanks for helpful answer. First, I tried WYSIWYG editors; but the problem is that I want to keep only few html tags; and I do not know a WYSIWYG editor to allow such customization (to keep bold tag only, for example). – Googlebot Feb 28 '12 at 12:09
  • 1
    Perfectly possible with some configuration: http://stackoverflow.com/questions/2912805/how-to-define-allowed-tags-in-ckeditor Though it doesn't look like CKEditor uses `` tags, it defines an alias to `` by default: http://svn.ckeditor.com/CKEditor/trunk/_source/plugins/styles/styles/default.js – Karl Barker Feb 28 '12 at 12:19
  • If you don't want/need an **editor** but just want to capture pasted HTML text (which you're free to then clean up as you wish, with all the challenges you'll face along the way) then see http://stackoverflow.com/a/22353003/623519 – artfulrobot Mar 12 '14 at 13:29
0

for future reference ...

Some form of formatting can be achieved through Unicode.
You can paste ' '.

Because there is not any style there at all. They are just unicode characters! So all you have to is find the suitable unicode character code for your intended style and convert your string to that.

https://emojistock.com/bold-italic-text-generator/
this site may help