HTML document object utility.
const {setElement} = require('@taufik-nurrohman/document');
console.log(setElement('div', 'Content goes here.', {
'class': 'bar foo',
'id': 'baz'
}));
import {setElement} from '@taufik-nurrohman/document';
console.log(setElement('div', 'Content goes here.', {
'class': 'bar foo',
'id': 'baz'
}));
Convert element to array of element name, element content and element attributes.
Get element name.
Get element’s parent.
Get closest <form>
element from current element.
// Remove all attribute(s)
letAttributes(node);
// Remove `class` and `id` attribute only
letAttributes(node, ['class', 'id']);
// Remove `class` and `id` attribute only
const removeValueAttribute = false;
letAttributes(node, {
'class': true,
'id': true,
'value': removeValueAttribute // Keep this attribute
});
// Remove all class(es)
letClasses(node);
// Remove `bar` and `foo` class only
letClasses(node, ['bar', 'foo']);
// Remove `bar` and `foo` class only
const isButtonDisabled = false;
letClasses(node, {
'active': isButtonDisabled, // Keep this class
'bar': true,
'foo': true
});
// Remove all `data-*` attribute(s)
letData(node);
// Remove `data-bar` and `data-foo` attribute only
letData(node, ['bar', 'foo']);
// Remove `data-bar` and `data-foo` attribute only
const removeBazData = false;
letData(node, {
'bar': true,
'baz': removeBazData, // Keep this attribute
'foo': true
});
// Remove all style(s)
letStyles(node);
// Remove `left` and `top` style only
letStyles(node, ['left', 'top']);
// Remove `left` and `top` style only
const removePositionStyle = false;
letStyles(node, {
'left': true,
'position': removePositionStyle, // Keep this style
'top': true
});
// Add `bar` and `foo` attribute(s)
// Remove the `baz` attribute if exists
setAttributes(node, {
'bar': 100,
'baz': false,
'foo': 1
});
// Set class value(s) to `['bar', 'foo']`
setClasses(node, 'bar foo');
// Add `bar` and `foo` to the class value(s)
setClasses(node, ['bar', 'foo']);
// Add `bar` and `foo` to the class value(s)
// Remove the `baz` value if exists
setClasses(node, {
'bar': true,
'baz': false,
'foo': true
});
// Add `data-bar` and `data-foo` attribute(s)
// Remove the `data-baz` attribute if exists
setData(node, {
'bar': 100,
'baz': false,
'foo': 1
});
Create a HTML element or update the existing HTML element.
console.log(setElement('div', 'Content goes here.', {
'id': 'foo-bar'
}));
console.log(setElement('input', false, {
'name': 'title',
'type': 'text',
'value': 'Title Goes Here'
}));
console.log(setElement('input', {
'name': 'title',
'type': 'text',
'value': 'Title Goes Here'
}));
let input = getElement('input[name="title"][type="text"]');
console.log(setElement(input, {
'value': 'Title Goes Here'
}));
// Add `left` and `top` style(s)
// Remove `bottom` and `right` style(s) if exist
setStyles(node, {
'bottom': false,
'left': 0,
'right': false,
'top': 0
});
Convert array of element name, element content and element attributes to element.
Get element’s outer HTML.
Toggle element state if available.
const button = getElement('button');
const details = getElement('details');
button.addEventListener('click', () => {
toggleState(details, 'open');
});
Return the <body>
element.
Return the document
object.
Return the <head>
element.
Return the <html>
element.
Return the window
object.
Return the window.history
object.
Return the window.location
object.
Return the current <script>
element.