0

I want to create a simple WYSIWYG editor with contenteditable and a bold option. This used to be really easy to achieve using execCommand as can be seen here.

Unfortunately execCommand is now deprecated.

My approach to achieve this without execCommand would be the following:

  1. Get selection with window.getSelection()
  2. Convert the selection to a text node on which I can set innerHTML and make sure it is a descendant of the contenteditable div
  3. Do a simple transform of the innerHTML (e.g. node.innerHTML = '<b>' + node.innerHTML + '</b>')

However, I am not sure how to achieve the second part. window.getSelection().anchorNode returns the correct node, but it won't let me set innerHTML or innerText on it.

Is this even possible? Thanks in advance!

Ood
  • 1,445
  • 4
  • 23
  • 43
  • https://stackoverflow.com/questions/66059594/how-to-create-a-richtext-editor-in-html-js-without-document-execcommand – Kip Apr 26 '22 at 14:14
  • Thanks, unfortunately the tags are not parsed in the only answer provided here. I don't want the user to see the HTML tags. – Ood Apr 26 '22 at 14:18
  • Although execCommand is deprecated you can still keep using it since there is no alternative. https://stackoverflow.com/a/70831583/1510309 – Kip Apr 26 '22 at 15:36

0 Answers0