DEV Community

loading...

Discussion on: How do you split contenteditable text preserving html formatting? [solved]

Collapse
merrimanxyz profile image
merriman-xyz • Edited

Thanks for posting this.

Worked wonderfully until I tried splitting a content editable that ended in bold text.

Looked at the code and recognized that it could only handle cases where firstChild and lastChild are text nodes.

Made a few tweaks to fix:

  • previousRange.setStart(firstChild, 0); becomes previousRange.setStartBefore(firstChild);
  • nextRange.setEnd(lastChild, lastChild.length); becomes nextRange.setEndAfter(lastChild);