How to keep the text format same from in textarea

699 views Asked by At

I am inserting some text using textarea. I want to substring the textarea once it reaches the limit and set the content to the same editor. But its removing the formatting before whatever is texted and setting plain text. I want with same format.

   tinyMCE.init({
        mode: "textareas",
        theme: "advanced",
        editor_selector: "mceEditor",
        paste_auto_cleanup_on_paste: 'true',
        theme_advanced_disable: 'justifyleft,justifyright,justifyfull,justifycenter,indent,image,anchor,sub,sup,unlink,outdent,help,removeformat,link,fontselect,hr,styleselect,formatselect,charmap,separator,code,visualaid,strikethrough,fullscreen',
        theme_advanced_buttons1: 'bold,italic,underline,numlist,bullist,undo,redo,cleanup,spellchecker',
        theme_advanced_buttons2: "",
        theme_advanced_buttons3: "",
        plugins: 'spellchecker,fullscreen,paste',
        spellchecker_languages: '+English=en-us',
        spellchecker_rpc_url: '<%out.print(request.getContextPath());%>/jazzy-spellchecker',
        theme_advanced_statusbar_location: "bottom",
        theme_advanced_path: false,
        statusbar: true,
        setup: function (editor) {
            editor.onKeyUp.add(function (evt) {
                var inputRichTextArea = $(editor.getBody()).text();
                var maxLengthRichTextArea = document.getElementById(editor.id).maxLength;
                var inputRichTextAreaLength = inputRichTextArea.length;
                if (inputRichTextAreaLength > maxLengthRichTextArea) {
                    inputRichTextAreaLength = maxLengthRichTextArea;
                    editor.setContent("");
                    tinyMCE.activeEditor.selection.setContent(inputRichTextArea.substring(0, maxLengthRichTextArea));
                }
                var value = maxLengthRichTextArea - inputRichTextAreaLength;
                if (value >= 0) {
                    $(editor.getContainer()).find("#" + editor.id + "_path_row").html("Remaining characters: " + (value));
                }
                if (inputRichTextAreaLength >= maxLengthRichTextArea) {
                    $(editor.getBody()).keypress(function (e) {
                        inputRichTextAreaLength = $(editor.getBody()).text().length;
                        if (inputRichTextAreaLength >= maxLengthRichTextArea) {
                            e.stopPropagation();
                            return false;
                        }
                        var value = maxLengthRichTextArea - inputRichTextAreaLength;
                        if (value >= 0) {
                            $(tinyMCE.activeEditor.getContainer()).find("#" + editor.id + "_path_row").html("Remaining characters: " + (value));
                        }
                    });
                }
            });
        }

    });
<textarea maxlength = 50 rows="4" cols="50"></textarea

How can I keep the same formatting.

Pasting text

Formatting text

1

There are 1 answers

0
Felix Haeberle On

In your example, you use the function .text() to get the content which only selects the text without format. Try using .getContent() instead, as it selects the whole content.

There are already question and answers about it here on StackOverflow. Please follow the instructions there.