What is dom-helpers?
The dom-helpers package is a collection of utility functions for managing and interacting with the DOM. It provides a consistent and cross-browser way to perform common DOM manipulation tasks without relying on a larger library like jQuery.
What are dom-helpers's main functionalities?
Class Manipulation
Allows adding and removing CSS classes from DOM elements.
import { addClass, removeClass } from 'dom-helpers';
const element = document.getElementById('my-element');
addClass(element, 'new-class');
removeClass(element, 'old-class');
Events
Facilitates adding and removing event listeners to DOM elements.
import { on, off } from 'dom-helpers';
const handleClick = event => console.log('Clicked!', event);
const element = document.getElementById('my-button');
on(element, 'click', handleClick);
// Later on, to remove the event listener
off(element, 'click', handleClick);
Query
Provides a way to select DOM elements using CSS selectors.
import { querySelectorAll } from 'dom-helpers';
const items = querySelectorAll(document, '.list-item');
console.log(items);
Style
Enables getting and setting CSS styles on DOM elements.
import { css } from 'dom-helpers';
const element = document.getElementById('my-element');
css(element, { display: 'none' });
css(element, 'display', 'block');
Dimensions
Allows measuring and setting the dimensions of DOM elements.
import { height, width } from 'dom-helpers';
const element = document.getElementById('my-element');
console.log(height(element));
console.log(width(element));
Other packages similar to dom-helpers
jquery
jQuery is a fast, small, and feature-rich JavaScript library. It makes things like HTML document traversal and manipulation, event handling, and animation much simpler with an easy-to-use API that works across a multitude of browsers. Compared to dom-helpers, jQuery is a more comprehensive tool but is also larger in size.
zepto
Zepto is a minimalist JavaScript library for modern browsers with a largely jQuery-compatible API. If you use jQuery, you will already be familiar with Zepto. While similar in API to dom-helpers, Zepto focuses on a jQuery-like experience in a smaller package.
cash-dom
Cash is an absurdly small jQuery alternative for modern browsers. It provides jQuery-style syntax for manipulating the DOM, handling events, and making AJAX requests. Cash-dom is more feature-rich than dom-helpers but still aims to be lightweight.
dom-helpers
tiny modular DOM lib for ie9+
Install
npm i -S dom-helpers
Mostly just naive wrappers around common DOM API inconsistencies, Cross browser work is minimal and mostly taken from jQuery. This library doesn't do a lot to normalize behavior across browsers, it mostly seeks to provide a common interface, and eliminate the need to write the same damn if (ie9)
statements in every project.
For example on()
works in all browsers ie9+ but it uses the native event system so actual event oddities will continue to exist. If you need robust cross-browser support, use jQuery. If you are just tired of rewriting:
if (document.addEventListener)
return (node, eventName, handler, capture) =>
node.addEventListener(eventName, handler, capture || false)
else if (document.attachEvent)
return (node, eventName, handler) =>
node.attachEvent('on' + eventName, handler)
over and over again, or you need a ok getComputedStyle
polyfill but don't want to include all of jQuery, use this.
dom-helpers does expect certain, polyfillable, es5 features to be present for which you can use es5-shim
where needed
The real advantage to this collection is that any method can be required individually, meaning bundlers like webpack will only include the exact methods you use. This is great for environments where jQuery doesn't make sense, such as React
where you only occasionally need to do direct DOM manipulation.
All methods are exported as a flat namesapce
var helpers = require('dom-helpers')
var offset = require('dom-helpers/offset')
require('dom-helpers/css')(node, { width: '40px' })
- dom-helpers
ownerDocument(element)
: returns the element's document ownerownerWindow(element)
: returns the element's document windowactiveElement
: return focused element safelyquerySelectorAll(element, selector)
: optimized qsa, uses getElementBy{Id|TagName|ClassName}
if it can.contains(container, element)
height(element, useClientHeight)
width(element, useClientWidth)
matches(element, selector)
offset(element)
-> { top: Number, left: Number, height: Number, width: Number}
offsetParent(element)
: return the parent node that the element is offset fromposition(element, [offsetParent]
: return "offset" of the node to its offsetParent, optionally you can specify the offset parent if different than the "real" onescrollTop(element, [value])
scrollLeft(element, [value])
scrollParent(element)
addClass(element, className)
removeClass(element, className)
hasClass(element, className)
toggleClass(element, className)
style(element, propName)
or style(element, objectOfPropValues)
getComputedStyle(element)
-> getPropertyValue(name)
animate(node, properties, duration, easing, callback)
programmatically start css transitionstransitionEnd(node, handler, [duration], [padding])
listens for transition end, and ensures that the handler if called even if the transition fails to fire its end event. Will attempt to read duration from the element, otherwise one can be providedaddEventListener(node, eventName, handler, [options])
:removeEventListener(node, eventName, handler, [options])
:listen(node, eventName, handler, [options])
: wraps addEventlistener
and returns a function that calls removeEventListener
for youfilter(selector, fn)
: returns a function handler that only fires when the target matches or is contained in the selector ex: on(list, 'click', filter('li > a', handler))
requestAnimationFrame(cb)
returns an ID for cancelingcancelAnimationFrame(id)
scrollbarSize([recalc])
returns the scrollbar's width size in pixelsscrollTo(element, [scrollParent])