The queryCommandEnabled() method is used to check whether the specified command is enabled by the browser or not.
Syntax:
isEnabled = document.queryCommandEnabled( command );
Parameters:
- command: It is the command for which support has to be determined.
Return value: It returns a Boolean value that specifies if the command is enabled by the browser. It returns true if the command is enabled and false if the command is disabled.
Example 1: This example shows if the “selectAll” command is enabled or not. We can use this information to execute the command using the execCommand() method or show a message to the user if it is not supported.
HTML
< html > <!DOCTYPE html> < html > < head > < title > HTML DOM queryCommandEnabled() method </ title > </ head > < body > < h1 >neveropen</ h1 > < p > A< br > B< br > </ p > < button onclick = "checkCommand()" > Click </ button > < script > function checkCommand() { // Check if the command is // enabled using the // queryCommandEnabled() method var isEnabled = document .queryCommandEnabled("SelectAll"); // Show the output to the console console.log(isEnabled); // Execute the command if the // task is enabled if (isEnabled) { document.execCommand("SelectAll", false, null); } } </ script > </ body > </ html > |
Output:
Before Clicking the Button:
After Clicking the Button:
Example 2: This example shows the case when this method will return false as the given command is not a valid one.
HTML
<!DOCTYPE html> < html > < head > < title > HTML DOM queryCommandEnabled() method </ title > </ head > < body > < h1 >neveropen</ h1 > < p > A< br > B< br > </ p > < button onclick = "checkCommand()" > Click </ button > < script > function checkCommand() { // Checking to see if an invalid // command is enabled var isEnabled = document.queryCommandEnabled("Select"); // Show the output to the console console.log(isEnabled); } </ script > </ body > </ html > |
-
Output:
Before Clicking the Button:
-
After Clicking the Button:
Supported Browsers::
- Google Chrome
- Edge 12
- Firefox 41
- Safari
- Opera
- Internet Explorer