Document: queryCommandState() method
Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see thecompatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.
Non-standard: This feature is not standardized. We do not recommend using non-standard features in production, as they have limited browser support, and may change or be removed. However, they can be a suitable alternative in specific cases where no standard option exists.
Note:Although theexecCommand() method is deprecated, there are still some valid use cases that do not yet have viable alternatives, as mentioned in theexecCommand() article. In these cases, you may find this method useful to implement a complete user experience, but test to ensure cross-browser compatibility.
ThequeryCommandState() method will tell you if the current selection has a certainDocument.execCommand() command applied.
In this article
Syntax
queryCommandState(command)Parameters
commandA command from
Document.execCommand()
Return value
queryCommandState() can return a boolean value ornull if the state is unknown.
Example
>HTML
<div contenteditable="true">Select a part of this text!</div><button>Test the state of the 'bold' command</button><hr /><div></div>hr,button { margin: 1rem 0;}JavaScript
function makeBold() { const state = document.queryCommandState("bold"); let message; switch (state) { case true: message = "The bold formatting will be removed from the selected text."; break; case false: message = "The selected text will be displayed in bold."; break; default: message = "The state of the 'bold' command is indeterminable."; break; } document.querySelector("#output").textContent = `Output: ${message}`; document.execCommand("bold");}document.querySelector("button").addEventListener("click", makeBold);Result
Specifications
This feature is not part of any current specification. It is no longer on track to become a standard. There is an unofficialW3C execCommand spec draft.
Browser compatibility
See also
HTMLElement.contentEditabledocument.designModedocument.execCommand()- Browser bugs related to
queryCommandState():Scribe's "Browser Inconsistencies" documentation