Skip to content

Instantly share code, notes, and snippets.

@vikynandha-zz
Last active August 30, 2017 08:38
Show Gist options
  • Save vikynandha-zz/6539809 to your computer and use it in GitHub Desktop.
Save vikynandha-zz/6539809 to your computer and use it in GitHub Desktop.
Check if given object is a DOM element
function isElement(obj) {
try {
//Using W3 DOM2 (works for FF, Opera and Chrom)
return obj instanceof HTMLElement;
}
catch(e){
//Browsers not supporting W3 DOM2 don't have HTMLElement and
//an exception is thrown and we end up here. Testing some
//properties that all elements have. (works on IE7)
return (typeof obj==="object") &&
(obj.nodeType===1) && (typeof obj.style === "object") &&
(typeof obj.ownerDocument ==="object");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment