Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

on pressing enter in contenteditable its copying line style/title to next line

I have simple contententeditable div

<div contenteditable="true">
  <p>
    Line 1 <span title="action">action</span>
  </p>
</div>

If the user click at the end of line 1 and press enter, then start typing. The next line also copies the same span’s background. How to avoid it?
see demo: https://jsfiddle.net/89z5d2r0/1/

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can add a non-breaking space or &nbsp after the span tag to circumvent this behavior. See the snippet below:

div {
  width: 200px;
  height: 200px;
  border: 1px solid #f1f2f3;
}

p span {
  background: #ccc;
}
<div contenteditable="true">
  <p>
    Line 1 <span title="action">action</span>&nbsp
  </p>
</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading