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

99
Views
Insert text in editable div when another div is clicked

I have an editable div and I want to insert some text when another div is clicked, using document.execCommand. When I use a button, it works properly, but it doesn't work when a clickable div is used.

<!-- When I click this, it works -->
<button on:click={e => document.execCommand("insertText", false, "Some text")}>Inser text</button>

<!-- When I click this, it doesn't -->
<div on:click={e => document.execCommand("insertText", false, "Some text")}>Inser text</div>

<div contenteditable="true" />

I suspect it's a focus issue. I have tried calling e.preventDefault() but it doesn't work either.

It's not relevant but I'm using Svelte.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I found a solution in this post.

Basically, mousedown event should be used, instead of click.

In my case, it looks like this:

<div on:mousedown={e => {
  e.preventDefault();
  document.execCommand("insertText", false, "Some text");
}>Inser text</div>
about 4 years ago · Juan Pablo Isaza Report
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!