JSPM

phane-tech-dom-utils

1.0.7
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 25
  • Score
    100M100P100Q49118F
  • License MIT

Pure JavaScript DOM utility functions

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 (undefined for 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