Apply JavaScript code to your dynamic DOM, automatically executed each time a new matching element is added.
npm i @itrocks/build
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.
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.
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)
-
selector:
A string with one or more CSS selectors to match.
For complex multiple-selectors, you may use a CSS selectors chain.
Defaults to the ALWAYS constant, triggering the callback for any element added to the DOM. -
type:
A case-sensitive string representing the event type to listen for.
Defaults to the CALL constant, which calls the callback immediately when an element is added to the DOM. -
event:
A
BuildEvent
object that configures the build event manager. Properties includeselector
,type
,callback
,args
,priority
.- args: Additional arguments to pass to the callback.
- priority: Defaults to 1000. If multiple build events match the same element, you can prioritize them by setting different values.
- Other properties correspond to
build()
parameters.
-
callback:
The function to execute.
callback(element, ...args)
- element: The DOM element that triggered the callback.
- args: Additional arguments associated with the build event.
- 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.
This feature avoids repeating CSS paths within complex CSS selector strings.
This selector string with repeated CSS path body > main
:
'body > main > header > h2, body > main > h2'
Is equivalent to:
['body > main', '> header > h2, > h2']