kicaj
kicaj

Reputation: 2968

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?

Upvotes: 0

Views: 1670

Answers (2)

Mohamad Hamouday
Mohamad Hamouday

Reputation: 2763

Did you try this? It's get style values

var selection = window.getSelection();
console.log(selection.anchorNode.parentNode.style);

And this get tag name:

var selection = window.getSelection();
console.log(selection.anchorNode.parentNode.tagName);

Upvotes: 0

rgthree
rgthree

Reputation: 7273

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

Upvotes: 3

Related Questions