DOM Element Checker

1.1.4 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Demonstrates how to use `isElement` to check various JavaScript values, including HTML and SVG elements, and non-element types, correctly distinguishing them from actual DOM elements.

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

view raw JSON →