Package Exports
- phane-tech-dom-utils
- phane-tech-dom-utils/index.cjs
- phane-tech-dom-utils/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (phane-tech-dom-utils) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
๐ฆ Kotipalli Phaneendra Kumar - DOM Utilities
A lightweight, safe, and predictable DOM utility module that simplifies common DOM operations with strict validation, consistent return values, and clean JSDoc documentation.
Designed specifically for browser environments, this module helps you work confidently with elements, attributes, classes, styles, events, and dynamic DOM creation without unexpected runtime errors.
โจ Highlights
- ๐งฑ Safe DOM selectors with validation
- ๐ท๏ธ Attribute helpers (get / set / remove)
- ๐งฉ Class utilities (add / remove / toggle / check)
- ๐จ Style helpers (single & multiple styles)
- ๐งฉ Dynamic element creation & DOM insertion
- ๐ง Event listener helpers
- โญ Tab title & favicon utilities
- ๐ Fully documented using JSDoc
- โก Predictable return values (
undefinedfor invalid input)
๐ฆ Installation
npm install phane-tech-dom-utils๐ Import
import {
getElementById,
getElementsByClassName,
getElementsByTagName,
setTabTitle,
getTabTitle,
setFavicon,
getFavicon,
setTextContent,
setHTMLContent,
setAttribute,
getAttribute,
removeAttribute,
hasClass,
addClass,
removeClass,
toggleClass,
setStyle,
setStyles,
createDynamicElement,
appendDynamicTag,
removeElement,
addEventListenerHelper,
removeEventListenerHelper,
onElementLoad,
setDataId
} from "phane-tech-dom-utils";๐ API Reference
๐ getElementById(id)
Safely returns a DOM element by its ID.
getElementById("header"); // HTMLElement | null
getElementById("unknown"); // null
getElementById(""); // undefined๐ getElementsByClassName(className)
Returns a live HTMLCollection of elements.
getElementsByClassName("card"); // HTMLCollection๐ getElementsByTagName(tagName)
Returns a live HTMLCollection of elements.
getElementsByTagName("div"); // HTMLCollection๐ท๏ธ setTabTitle(title)
Sets the browser tab title.
setTabTitle("Home"); // "Home"โญ getTabTitle()
Returns the current tab title.
getTabTitle(); // "Home"โญ setFavicon(url)
Sets or updates the browser favicon.
setFavicon("/favicon.ico");โญ getFavicon()
Returns the current favicon URL if available.
getFavicon(); // string | undefinedโ๏ธ setTextContent(element, text)
Safely sets innerText on an element.
setTextContent(el, "Hello World");โ๏ธ setHTMLContent(element, html)
Safely sets innerHTML on an element.
setHTMLContent(el, "<b>Hello</b>");๐งฑ setAttribute(element, name, value)
Adds or updates an attribute.
setAttribute(el, "data-id", "123");๐งฑ getAttribute(element, name)
Gets an attribute value.
getAttribute(el, "data-id"); // "123"๐งฑ removeAttribute(element, name)
Removes an attribute.
removeAttribute(el, "disabled");๐งฉ hasClass(element, className)
Checks if a class exists on an element.
hasClass(el, "active"); // true | false๐งฉ addClass / removeClass / toggleClass
Manages element classes safely.
addClass(el, "active");
removeClass(el, "active");
toggleClass(el, "active");๐จ setStyle(element, property, value)
Sets a single CSS style.
setStyle(el, "color", "red");๐จ setStyles(element, styles)
Applies multiple CSS styles at once.
setStyles(el, {
color: "red",
backgroundColor: "yellow"
});๐งฉ createDynamicElement(tag, content?, props?)
Creates a DOM element dynamically.
createDynamicElement("div", "Hello", {
id: "box",
className: "card"
});โ appendDynamicTag(child, parent?)
Appends an element to the DOM.
appendDynamicTag(div);
appendDynamicTag(div, container);โ removeElement(child, parent?)
Removes an element from the DOM.
removeElement(div);๐ง addEventListenerHelper(el, type, handler)
Adds an event listener safely.
addEventListenerHelper(btn, "click", handleClick);๐ง removeEventListenerHelper(el, type, handler)
Removes an event listener safely.
removeEventListenerHelper(btn, "click", handleClick);โณ onElementLoad(element?, handler)
Executes a handler when DOM or element is ready.
onElementLoad(null, () => console.log("DOM Ready"));๐ setDataId(element, value)
Sets data-id attribute.
setDataId(el, "123");๐งช Design Principles
- โ Invalid input โ undefined
- โ Safe DOM access
- ๐ No unexpected mutations
- ๐ Predictable, documented behavior
๐ License
MIT
๐ Links
- GitHub Repository: https://github.com/phane-tech/phane-tech-dom-utilities
- Demo / Documentation: https://phane-tech.github.io/phane-tech-dom-utilities/module-DomHelpers.html
- Unit Test Cases Reports: https://phane-tech.github.io/phane-tech-dom-utilities/unit-test-report.html