DOM Element Checker
The `iselement` package provides a focused utility function to accurately determine if a given JavaScript object is a DOM `Element`. Unlike simpler checks that rely solely on `instanceof HTMLElement`, this library is designed to correctly identify a broader range of elements, including 'exotic' elements such as SVG polygons. This ensures more robust and cross-browser compatible DOM manipulation logic, particularly crucial when dealing with complex web applications or third-party content. The current stable version is 1.1.4. Given its specific utility, the release cadence is typically stable and infrequent, with updates primarily for bug fixes or minor enhancements rather than new feature introductions. Its key differentiator is its comprehensive approach to element identification beyond standard HTML elements, providing reliable checks across the entire DOM specification.
Common errors
-
TypeError: isElement is not a function
cause Attempting to import `isElement` as a named export (`import { isElement } from 'iselement';`) when it is a default export, or destructuing `require('iselement')`.fixFor ESM, change to `import isElement from 'iselement';`. For CommonJS, change to `const isElement = require('iselement');`. -
ReferenceError: HTMLElement is not defined
cause Attempting to use `iselement` in a pure Node.js environment without a DOM emulation layer like JSDOM, where browser-specific global objects are absent.fixEnsure `iselement` is executed in a browser context or in a Node.js environment that has JSDOM or similar global DOM objects configured. It is not intended for pure server-side logic.
Warnings
- gotcha This utility specifically checks for `Element` nodes, not all `Node` types. Text nodes, attribute nodes, or document fragments are `Node`s but not `Element`s, and `isElement` will return `false` for them. Ensure your logic correctly distinguishes between `Node` and `Element` if you expect to handle other node types.
- gotcha The `iselement` function is designed for browser environments and relies on the presence of the DOM API (e.g., `document`, `Element`). Running it in a pure Node.js environment without a DOM emulation library (like JSDOM) will likely result in `ReferenceError`s or incorrect behavior, as global objects like `HTMLElement` or `Element` might not be defined.
Install
-
npm install iselement -
yarn add iselement -
pnpm add iselement
Imports
- isElement
import { isElement } from 'iselement';import isElement from 'iselement';
- isElement
const { isElement } = require('iselement');const isElement = require('iselement');
Quickstart
import isElement from 'iselement';
// Example 1: Regular HTML element
const div = document.createElement('div');
console.log('Is div an element?', isElement(div)); // true
// Example 2: SVG element (a common edge case for instanceof HTMLElement)
const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
const circle = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
console.log('Is SVG element an element?', isElement(svg)); // true
console.log('Is SVG circle an element?', isElement(circle)); // true
// Example 3: Non-element objects
const obj = {};
const str = 'hello';
const num = 123;
console.log('Is plain object an element?', isElement(obj)); // false
console.log('Is string an element?', isElement(str)); // false
console.log('Is number an element?', isElement(num)); // false
// Example 4: Text node (not an Element, but a Node)
const textNode = document.createTextNode('Hello');
console.log('Is text node an element?', isElement(textNode)); // false