JQuery Texteditor: keep format when I copy & paste text from somewhere else

2.6k views Asked by At

jQuery Text Editor: Remove pasted formatting that is not allowed

I am using the jQuery Text Editor, a wysiwyg editor to create rich formatted HTML in a textarea input.

I can initiate the editor in my javascript code and allow / disallow certain formatting options. But when I copy & paste text from somewhere else (Word, E-Mail client, etc.), it keeps all the formatting, even if I disallowed this particular formatting option in the editor.

Is there any smart way to strip all HTML formatting that I disallowed? Or even better can I tell the editor to strip disallowed formatting automatically that is pasted?

2

There are 2 answers

0
Ghost Rider On

Try this.

$(document).on("paste", ".jqte_editor", function(e) {

    e.preventDefault();
    var text = e.originalEvent.clipboardData.getData("text/plain");

    //Do required strip of HTML elements

    document.execCommand("insertText", false, text);
});
0
Mohd Javed On
$(document).on("paste", ".jqte_editor", function(e) {

    e.preventDefault();
    var text = e.originalEvent.clipboardData.getData("text/plain");

    //Do required strip of HTML elements
    document.execCommand("insertText", false, text);
});

The above code I have tried but when I copied it from excel to jQuery richtext the formatting was lost.