Last Updated: September 01, 2020
·
82
· david262929

How to check if object DOM element

/**
 * Check is object a DOM element
 *
 * ... usage ...
 *      // isDomElement( element )
 *
 * @param elem
 * @returns {boolean|*}
 */
const isDomElement = (elem = null) => {
    try {
        if (typeof elem === "string" && elem != "") {
            elem = document.querySelector('#' + elem);
        }

        if (typeof elem === "undefined" || elem == null) {
            return false;
        }

        throw "Parameter is not a number!";
    } catch (e) {
        return (typeof HTMLElement === "object" ? elem instanceof HTMLElement : elem != "" && typeof elem === "object" && elem !== null && elem.nodeType === 1 && typeof elem.nodeName === "string" && typeof elem.ownerDocument === "object" && elem);
    }
};