.onRemove(fn)
Available on: InDom, InDomArray

Registers a callback function that runs after the object's internal state (listeners, data) has been cleaned up, and just before its element is removed from the DOM.

Parameters:
fn {(n: InDom) => void} - The callback function

Returns: {Function | Function[]} - The internal handler(s) – pass to .off('onRemove', …) to unregister

Throws:
TypeError - If fn is not a function
Error - If the underlying element(s) has been removed

Examples:
const example = $1('.example');

// callback fires no matter how the element is removed
example.onRemove(() => {
  console.log('removed:', example);
  alert('press OK → element disappears');
});

// Through InDom remove method on the object
example.remove();

// Through InDom setHtml on body 
$1('body').setHtml('empty');

// Through native DOM removal
document.querySelector('.example').remove();

// Through native innerHTML on its parent 
document.querySelector('.example').parentElement.innerHTML = 'empty';
Next: off »
Modern DOM Power
for TypeScript, ESM & Plain JS
3.8KB JavaScript library that simplifies DOM manipulation
with a clean, chainable API for events, data, inputs harvesting, and more.
Automatic Cleanup,
Leak-Proof by Design
Events and state are cleared when elements leave the DOM,
even if removal happens outside InDom.
Cleaner Code,
Better Ergonomics
Get the InDom object directly in callbacks.
One element, one instance.
Works With Your Existing Stack
Use InDom on its own or alongside any library or framework,
its architecture ensures a seamless integration.