I want to read if a certain text in a contenteditable element is bold or not. In Chrome document.queryCommandValue("bold")
returns "true"/"false"
as a string, IE returns a true/false
as a boolean, but Firefox returns (empty string)
in the developer console.
I made a fiddle as an example: /
If you write som text in the div, mark it and then hit "Bold" the span should show "true"/"false"
or true/false
. I don't really care if its as a string or a boolean as I can convert it later.
I want to read if a certain text in a contenteditable element is bold or not. In Chrome document.queryCommandValue("bold")
returns "true"/"false"
as a string, IE returns a true/false
as a boolean, but Firefox returns (empty string)
in the developer console.
I made a fiddle as an example: http://jsfiddle/nTQd2/
If you write som text in the div, mark it and then hit "Bold" the span should show "true"/"false"
or true/false
. I don't really care if its as a string or a boolean as I can convert it later.
2 Answers
Reset to default 10For bold, italic and similar, use document.queryCommandState()
. Use document.queryCommandValue()
for non-Boolean mands.
http://jsfiddle/nTQd2/1/
That's simple.
Document.queryCommandValue()
returns a string. Instead of that use this.
Document.queryCommandState('Bold')
This will return a boolean value as true/false.
Example:
function isSelectedTextBold()
{
var isBold = document.queryCommandState("Bold");
return isBold;
}
I am also specifying function calls for other formatting options.
- Italic -> document.queryCommandState("Italic");
- Underline -> document.queryCommandState("Underline");
- Left Justify -> document.queryCommandState("justifyLeft");
- Center Justify -> document.queryCommandState("justifyCenter");
- Right Justify -> document.queryCommandState("justifyRight");
I hope this helps. :)