Skip to content

Instantly share code, notes, and snippets.

@basshelal
Created June 3, 2019 09:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save basshelal/0e6dd320cec78888005052b1739fda2c to your computer and use it in GitHub Desktop.
Save basshelal/0e6dd320cec78888005052b1739fda2c to your computer and use it in GitHub Desktop.
Check whether a node is editable or not in TypeScript
/**
* Checks whether the passed in node is editable or not.
* An editable node is one that returns true to isContentEditable or has a tag name as
* any one of the following:
* "textarea", "input", "text", "email", "number", "search", "tel", "url", "password"
*
* @param node the node to check
* @return true if the node is editable and false otherwise
*/
function isEditable(node: Node): boolean {
let element = node as HTMLElement;
let nodeName: string = element.nodeName.toLowerCase();
let editables = ["textarea", "input", "text", "email", "number", "search", "tel", "url", "password"];
return (element.isContentEditable || (element.nodeType === Node.ELEMENT_NODE && editables.contains(nodeName)));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment