jsElements contains a couple of methods for handling DOM elements. The library was built with the goal of learning the best methods for interacting with Elements and is meant to be a lightweight replacement for jQuery. (Hat tip to zepto and other jQuery replacement libraries.)
The scope of this library covers two different concerns.
-
Classes - programatically changing state with classes.
-
Elements -
create,remove
Building the script requires grunt and npm
git clone ... cd [dir] npm install && grunt Individual modules can be added or removed from the Gruntfile.js concat configuration object: concat.dist.src.
// `pojo` var el = jsElements.createElement({ id: 'id', name: 'name' }) // `es6` import { createElements } from 'jsElements'; var el = createElement({ ... }); //div.append(el) ... Accepts an array of elements, and a string class name, and adds the name with either the classList method or the deprecating className property of the elements in the array.
// add a `section` class to all `sections` for performative selectors jsElements.addClassToElements(Array.prototype.splice.call(document.getElementsTag('section')), 'section'); Accepts an element and a class name.
jsElements.addClass(document.getElementById('id'), 'className'); Create an element using the createElement method. Any valid element property is a valid option, for instance name, id, value, etc.
A full list of element properties is available here
jsElements.createElement({ id: 'elementId', name: 'elementName' }); Accepts an element and a class name, and returns a boolean.
jsElements.hasClass(el, 'mouseIsCurrentlyHovering'); Accepts string class name and a function, and calls the function for each element - basically a map for elements.
jsElements.eachClassElement('section', (el) => { console.log(el); }) Accepts an element and a class name, and removes the class from the element.
jsElements.removeClass(el, 'name'); A backwards-compatible remove method. Accepts an element property.
jsElements.removeElement(el); Convert an html string to an element.
var el = jsElements.stringToElement('<span>a string containing html</span>');