Skip to content

Instantly share code, notes, and snippets.

@amadeuszblanik
Last active July 28, 2019 00: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 amadeuszblanik/f71fa4b8daa6c83dc5a62df7232e2408 to your computer and use it in GitHub Desktop.
Save amadeuszblanik/f71fa4b8daa6c83dc5a62df7232e2408 to your computer and use it in GitHub Desktop.
Test if provided element is DOM element
const isDOMelement = (element, nodeListAllowed = true) => {
if (typeof element !== "object" || element === null) {
console.debug("Element is not an object.\nIt's fine you are in debug mode.");
return false;
} else {
if (!element instanceof HTMLElement) {
console.warn("element is not an instanceof HTMLElement");
return false;
} else if (typeof element.classList !== "object") {
if (!NodeList.prototype.isPrototypeOf(element)) {
console.warn("Provided element isn't NodeList and HTMLElement instanceof");
return false;
} else {
if (!nodeListAllowed) {
console.debug("Provided element is NodeList and it allowed");
return false;
} else {
console.warn("Provide element is NodeList and it's allowed");
if(typeof element.addEventListener !== "undefined") {
console.warn("Provided element doesn't supports Event Listeners");
return false;
} else {
return true;
}
}
}
console.warn("classList of element is not an object");
return false;
} else {
return true;
}
}
console.error("Unknown error.");
return false;
}
export default isDOMelement;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment