Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@degjs/dom-utils

Package Overview
Dependencies
Maintainers
8
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@degjs/dom-utils

A utility library for working with the DOM.

  • 3.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
8
Created
Source

domUtils

Build Status

Working with the browser's Document Object Model (DOM) via JavaScript has historically been harder than it should be, filled with enough inconsistencies and browser bugs to drive even the best developer crazy. These challenges were a huge factor in the ubiquity of JavaScript helper libraries such as jQuery across the web.

Fortunately, those days are largely behind us. Modern-day DOM interaction is simpler and more consistent, to the point where jQuery shouldn't be an assumed dependency anymore (no, really).

domUtils bridges the gap between vanilla JS and a full-fledged library. It's a collection of helper methods that can be imported individually to keep your codebase lean, or together when more are needed.

Install

domUtils is an ES6 module. Consequently, you'll need an ES6 transpiler (Babel is a nice one) as part of your Javascript workflow.

If you're already using NPM for your project, you can install domUtils with the following command:

$ npm install @degjs/dom-utils

Usage

Importing individual domUtils methods:

import { createElement } from "@degjs/dom-utils";

let newEl = createElement('div', ['classNameA', 'classNameB']); // Create a new element
document.body.appendChild(newEl); // Add the new element to the DOM

Importing all domUtils methods:

import * as domUtils from "@degjs/dom-utils";

let newEl = domUtils.createElement('div', ['classNameA', 'classNameB']); // Create a new element
document.body.appendChild(newEl); // Add the new element to the DOM
domUtils.removeElements(newEl); // Remove the new element from the DOM

Methods

isElement(el)

The isElement method tests a supplied value to see if it's a valid HTML element, and returns true or false.

el

Type: Element
The potential element to test.

createElement(nodeName, classNames)

nodeName

Type: String
The name of the new HTML element you want to create (i.e., 'div', 'li', etc.).

classNames

Type: String or Array
An indivudal class name, or array of class names, that will be added to the returned element.

emptyElements(els)

The emptyElements method removes all child elements from the supplied list of HTML elements.

els

Type: Element or Array
A single HTML element or an array of HTML elements that will be emptied.

replaceContent(el, newContent)

The replaceContent method replaces an element's content with the supplied new content.

el

Type: Element
A single HTML element that will have its content replaced.

newContent

Type: String
The content that will replace the element's old content.

removeElements(els)

The removeElements method removes all supplied HTML elements from the DOM.

els

Type: Element or Array
A single HTML element or an array of HTML elements that will be removed.

wrapElements(elsToWrap, wrapperEl)

The wrapElements method wraps all supplied HTML elements within another supplied element.

elsToWrap

Type: Element or Array
A single HTML element or an array of HTML elements that will be wrapped by the wrapperEl element.

wrapperEl

Type: Element
A single HTML element that will be wrapped around the supplied elsToWrap elements.

unwrapElements(wrapperEls)

The unwrapElements method removes a wrapping parent HTML element, leaving all of its child elements in place.

wrapperEls

Type: Element or Array
A single HTML element or an array of HTML elements that will be removed, without removing child elements.

Browser Support

domUtils depends on the following browser APIs:

To support legacy browsers, you'll need to include polyfills for the above APIs.

Keywords

FAQs

Package last updated on 17 Sep 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc