queryCommandState
Summary
Returns a Boolean value that indicates the current state of the command.
Method of dom/TextRangedom/TextRange
Syntax
var result = document.queryCommandState(/* see parameter list */);
Parameters
cmdID
- Data-type
- BSTR
String that specifies a command identifier.
Return Value
Returns an object of type BooleanBoolean
Boolean
Boolean that returns one of the following possible values:
| Return value | Description |
|---|---|
| true | The given command has been executed on the object. |
| false | The given command has not been executed on the object. |
Examples
This example feature tests for window.getSelection and if not available (legacy IE versions) falls back to document.selection to create a range object. Then queryCommandState is used to determine if the range object can execute the 'bold' command.
function makeBold(el){
if(window.getSelection){
var sel=document.getSelection();
var range = sel.getRangeAt(0),
content = range.extractContents(),
span = document.createElement('b');
span.appendChild(content);
var htmlContent = span.innerHTML;
range.insertNode(span);
}else{
var sel=document.selection;
if(sel){
var rng=sel.createRange();
if(rng.queryCommandEnabled('bold')){rng.execCommand('bold',false,null);}
}
}
}
Usage
Used to add HTML markup to web documents.
Notes
Remarks
This method is a wrapper function for the command constants. You can obtain an IHTMLDocument2 interface using IID_IHTMLDocument2 for the IID. This method is a wrapper function for the command constants. You can obtain an IHTMLControlRange interface using IID_IHTMLControlRange for the IID. This method is a wrapper function for the command constants. You can obtain an IHTMLTxtRange interface using IID_IHTMLTxtRange for the IID.
Attributions
Microsoft Developer Network: [queryCommandState Method Article]