7

Possible Duplicate:
Identify whether the selected text in a web page is bold nor not

I've created a contenteditable div, and this was straightforward enough.

I have as well added a control on top of the div to bold and unbold the text.

Now, I would like to style the button based on the bold status of the text under the cursor - ie: if the text under the cursor is bold, I would like the bold icon to have border 'inset'... and if it's not bold, I would like the bold icon to be without a border.

How can I check the style of the element under the cursor?

Link to the code as it is now: http://jsfiddle.net/aCp7M/

Community
  • 1
  • 1
Alterlife
  • 6,557
  • 7
  • 36
  • 49

1 Answers1

12

Here you are: http://jsfiddle.net/vvCzD/

Use document.queryCommandValue("Bold") to check that text at the caret position is bold and so on. All possible command identifiers are described here.


UPD: As Tim stated below, a correct method for getting boolean-like properties is queryCommandState.

Igor Shastin
  • 2,878
  • 2
  • 25
  • 38