0

The problem only happens in Google Chrome, it works fine in Firefox.

Text Simple without format:

select its text and paste
Title Example
parrafe

parrafe

parrafe


New Line 
Other Line

When paste in contenteditable, Chrome my texts paste return:

<div>Title Example
</div><div>parrafe
</div><div>
</div><div>parrafe
</div><div>
</div><div>parrafe
</div><div>
</div><div>
</div><div>New Line 
</div><div>Other Line</div>

How prevent, my text wrappin in ? Chrome should put line breaks

My code Html:

<article contenteditable="true">
    Paste Here
</article>
<section>
    <div>
        <span>
            <h1>select its text and paste</h1>
            <h2>Title Example</h2>
            <p>parrafe</p>
            <p>parrafe</p>
            <p>parrafe</p>
            <br/>
            New Line
            <br/>
            Other Line
        </span>
    </div>
</section>

My jQuery code:

 $('[contenteditable]').on('paste', function(e) {

        e.preventDefault();

        var text = '';

        if (e.clipboardData || e.originalEvent.clipboardData) {
            text = (e.originalEvent || e).clipboardData.getData('text/plain');
        } else if (window.clipboardData) {
            text = window.clipboardData.getData('Text');
        }
        text = text.replace(/<[^>]*>?/gm, '');

        if (document.queryCommandSupported('insertText')) {
            document.execCommand('insertText', false, text);
        } else {
            document.execCommand('paste', false, text);
        }

    });

$('[contenteditable]').keydown(function(e) {
   if (e.keyCode === 13) {
        document.execCommand('insertHTML', false, '<br><br>');
     return false;
   }
});

My jsFiddle:

https://jsfiddle.net/qnu47L5a/

Souleste
  • 1,887
  • 1
  • 12
  • 39
  • 2
    Could you be a bit more clear about what result you want? Do you not want the `
    ` tags to be included? And what is the output when you paste into Firefox?
    – MichaelM Sep 20 '19 at 16:22
  • 1
    In your `keydown` function, try using 'insertText', rather than 'insertHTML' – Souleste Sep 20 '19 at 16:32
  • @MickelsonMichael I want the pasted text not to insert the
    tags. In Firefox return text whitout of divs, Firefox insert
    , I need that.
    – Renato Ramos Puma Sep 20 '19 at 16:50
  • @RenatoRamosPuma check out this question for possible solutions https://stackoverflow.com/questions/6023307/dealing-with-line-breaks-on-contenteditable-div – MichaelM Sep 20 '19 at 16:54
  • Firefox return plain text: "
    Title Example

    parrafe

    parrafe

    parrafe

    New Line
    Other Line
    ". I need the same, without
    – Renato Ramos Puma Sep 20 '19 at 16:56

0 Answers0