DOM helpers
dom.js replaces the contents of interface containers.
- dom.inject_html(selector, html)
Replace an element's contents with HTML and execute inserted scripts.
Existing contents are removed before insertion. Script elements are replaced with newly created elements so that both inline and external scripts run.
- Arguments:
selector (string) -- CSS selector for the target element.
html (string) -- HTML markup to insert.
- Throws:
TypeError -- If the selector does not match an element.
- Returns:
void
Example
inject_html('#message', '<strong>Ready</strong>');
- dom.inject_node(selector, node)
Replace an element's contents with a DOM node.
Passing a falsy node clears the target without appending a replacement.
- Arguments:
selector (string) -- CSS selector for the target element.
node (Node) -- Node to append, or a falsy value to only clear the target.
- Throws:
TypeError -- If the selector does not match an element.
- Returns:
Node -- The supplied node.
Example
const status = document.createElement('span'); status.textContent = 'Ready'; inject_node('#message', status);