1.0.1 • Published 3 months ago

dom-change-event v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

dom-change-event

NPM version Downloads Detect all changes in the DOM (added elements, removed elements, attributes updates)

Installation

npm i dom-change-event

Usage

import DomChangeEvent from 'dom-change-event';
DomChangeEvent(); // init the listener

// you can add these listeners for all HTML elements
document.body.addEventListener("addedNodes", (e) => {
    console.log("addedNodes in body element", e.detail);
});

document.body.addEventListener("removedNodes", (e) => {
    console.log("removedNodes in body element", e.detail);
});

const div = document.createElement("div");
document.body.append(div);
div.addEventListener("attributeChange", (e) => {
    console.log("attributeChange", e.detail);
    console.log("attributeName", e.detail.attributeName);

    console.log("attributeOldValue", e.detail.oldValue);
    const htmlElement = e.detail.target;
    console.log("attributeNewValue", htmlElement.getAttribute(e.detail.attributeName));
});
div.setAttribute("myAttribute", "myValue");

setTimeout(() => {
    div.remove();
}, 2000);

Events list

EventDescriptionValue
addedNodesGet array of elements added in an HTMLElementArrayHTMLElement
removedNodesGet array of elements removed from an HTMLElementArrayHTMLElement
attributeChangeGet attribute change information from an HTMLElementattributeName: The name of the attributeoldValue: The old value attributetarget: the HTMLElement