Rich text editor, bold text using the strong tag

contenteditable, execcommand, javascript

Solution

Unfortunately `document.execCommand()` behaviour varies between browsers. As @1UnitedPower's answer mentions, `document.execCommand()` is intended for for presentational rather than semantic effect.

Two possible options are:

- Write your own code to do the bold styling. Unfortunately it is non-trivial to do this properly. You could look at how major WYSIWYG editors such as CKEditor and TinyMCE do it.

- Run some code to convert `<b>` elements into `<strong>` elements after calling `document.execCommand()`. This would seem the easier option. You will need some way to preserve the selection while doing the conversion, if that's important to you.

Problem

I'm working on a very very simple rich text editor. I've read about using designMode = 'On' applied to an iframe, and then I use this to create bold text: ``` nameOfiframe.document.execCommand('bold',false,null); ``` Even though it's works, execCommand() uses `b` tags instead of `strong` to make bold text. I took a look at some advanced rich text editor, all of them used `strong` instead of the `b` tag. Is there a simple way for me to fix this? Or are execCommand() not good to use at all? Thanks!

Original source