Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

171
Views
Javascript: How to prevent adding div on document.execcommand("insertText") on `contentEditable` element?

Is there a way to prevent auto adding div or any other tag when executing insertText on contentEditable div?

For instance I want to copy and paste a paragraph containing many newLine character:

Lorem ipsum dolor sit amet, 
consectetur adipiscing elit, 
sed do eiusmod tempor 
incididunt

it has 4 lines. When I copy and paste it into my contentEditable div, it became:

<div>Lorem ipsum dolor sit amet,</div>
<div>consectetur adipiscing elit,</div>
<div>sed do eiusmod tempor</div>
<div>incididunt</div>

I works fine with insertHTML, but the problem is that sometimes I want to copy and paste HTML elements too like:

<h1>Title</h1>
<p>Hello world</p>
<div>Horayy</div>

I do not want to create html elements inside my contentEditable div.

How can I achieve this?


NOTE

I can prevent adding div if I input enter key manually, but this case is different, I need to prevent it for copied text.

This is what I have done:

  const pastePlainText = useCallback((e: ClipboardEvent) => {
    e.preventDefault();

    if (e && e.clipboardData) {
      var text = e.clipboardData.getData('text/plain');
      document.execCommand("insertText", false, text);
    }
  }, []);
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!