build
Apply JavaScript code to your dynamic DOM,
automatically executed each time a new matching element is added.
Installation
npm i @itrocks/build
Usage
To execute a function each time a new element is added
to the DOM:
import build from './node_modules/@itrocks/build/build.js'
build<HTMLAnchorElement>('a', anchor => console.log('DOM + anchor', anchor))
This will display "DOM + anchor" in your console for every anchor already present in the DOM.
If you later add another anchor:
document.body.append(document.createElement('a'))
"DOM + anchor" will appear in your console again.
Demo, Testing and Development
git clone https://github.com/itrocks-ts/build
cd build
npm install
npm run build
To test, serve demo/build.html with a local web server and open your browser's console to see it in action.
API
build
Executes a callback each time a matching element is added to the DOM.
build(callback)
build(event)
build(selector, callback)
build(selector, event, callback)
build(selector, type, callback)
Parameters
Constants
- ALWAYS:
A universal selector that applies to any element added to the DOM.
- CALL:
A special event type that calls a callback immediately upon adding an element to the DOM.
CSS selectors chain
This feature avoids repeating CSS paths within complex
CSS selector strings.
Example
This selector string with repeated CSS path body > main
:
'body > main > header > h2, body > main > h2'
Is equivalent to:
['body > main', '> header > h2, > h2']