Search code examples
javascriptwysiwyg

Getting result from queryCommandValue on selected text


There is any solution how to return value from queryCommandValue for selected text?
I know about queryCommandValue('bold') return string true or false (not boolean!).
I try prepare some small WYSIWYG editor, but...

The question is how to get values from selected text, like as: the selected text is bolded and underlined?


Solution

  • No, there is no way. That's because queryCommandValue isn't telling you the state of the selected text, it's about the input. As in, if you select a paragraph that is both bold and not bold, queryCommandValue can't tell you that some of the text is bold and some of the text is not bold. Essentially, it tells you the value of the next input; in this example, if you press a character-key will that be bold or not (here, it will depend on where you started your selection: inside the bold format or not).

    Also, you should know you can use document.queryCommandState to get a boolean returned. Of course, it only works for boolean values, things like color, font you will still want to use queryCommandValue