Reputation: 133
All knows, that we can get selection of text in javascript by method
var range = window.getSelection ();
But, how to get style of this selection? When I select bolded text or italic, how to i can know about this?
(I have only one idea: get position of this selected text and get html for this position...)
Upvotes: 2
Views: 2190
Reputation: 4164
You can use document.queryCommandState()
for things like bold and italic and document.queryCommandValue()
for things like font size and style.
You must supply the flag to each method for it to give you a true or false value, it wont just return the current styles. So if you want to know if the selected text is bold you can write...
if(document.queryCommandState('Bold')){
// its bold!
}
This lists the variety of identifiers and methods
http://msdn.microsoft.com/en-us/library/ms533049(v=vs.85).aspx
Upvotes: 5
Reputation: 324597
You can use one of document.queryCommandState()
(see also dottoro) for commands such as "Bold" and "Italic" that have no value and document.queryCommandValue()
(see also dottoro) for commands that have a value, such as "FontName".
Upvotes: 2