d.js
Micro dom manipulation library. Because jQuery is not needed always.
- Compatible with modern browsers and IE 11
- Installable with NPM
npm install d_js
and bowerbower install d.js
- Compatible with AMD, commonJS and global javascript
- Only 4Kb (minified)
- HTML & SVG support
Usage example:
//Get all .buttons elementsvar buttons = d; //Change css propertiesd; //Handle eventsd;
API
d.get(query)
Returns the first element found:
- query A string with the selector, an object
{"selector": elementContext}
or a Node/NodeList
var container = d; //Use an object to specify the contextvar buttonInContainer = d;
d.getAll(query)
Returns NodeList
with all elements found:
- query A string with the selector or an object
{"selector": elementContext}
d;
d.getSiblings(element, query)
Returns an Array
with all siblings of another.
- element A string with the selector, an object
{"selector": elementContext}
or a Node/NodeList - query Optional string to filter the siblings
d; //return all siblingsd; //return all siblings with class '.filtered'
d.on(event, query, callback, useCapture)
Attach an event to the elements
- event A string with the event name or an instance of
Event
- query A string with the selector, an object
{"selector": elementContext}
or a Node/NodeList - callback The event callback
- useCapture (optional)
{ ;} d;
d.delegate(event, query, target, callback, useCapture)
Delegate an event to the elements
- event A string with the event name or an instance of
Event
- query A string with the selector, an object
{"selector": elementContext}
or a Node/NodeList - target A string with the target selector
- callback The event callback
- useCapture (optional)
{ ;} d;
d.off(event, query, callback, useCapture)
Removes an event from the elements
- event A string with the event name or an instance of
Event
- query A string with the selector, an object
{"selector": elementContext}
or a Node/NodeList - callback The event callback
- useCapture (optional)
d;
d.trigger(event, query)
Trigger an event of the elements
- event A string with the event name or an instance of
Event
- query A string with the selector, an object
{"selector": elementContext}
or a Node/NodeList
d;
d.data()
Set/get data-*
attributes. It can detect and convert primitive types like integers, floats and booleans. If it's an array or object, is converted to json.
- query A string with the selector, array of elements or a Node/NodeList/HTMLCollection instance
- name A string with the name of the data attribute, an array of names or an object with name/values
- value The new value of the property
//Get a data valuevar clicked = ddata'.button' 'clicked'; //Set a new valueddata'.button' 'clicked' true; //Set several valuesddata'.button' boolean: true array: 'blue' 'red' 'green' object: one: '1' two: 2 integer: 123 float: 12345; //Get some valuesvar someValues = ddata'.button' 'clicked' 'boolean' 'array'; //Get all valuesvar allValues = ddata'.button';
d.css()
Set/get the css properties of the first element. The vendor prefixes are handled automatically.
- query A string with the selector, array of elements or a NodeList/HTMLCollection instance
- prop A string with the property name or an object with property/values
- value The new value of the property
//Get the valuevar color = d; //Set a new valued; //Set several valuesd;
d.parse()
Parses html and returns a DocumentFragment
- html A string with the code to parse
//parse one elementvar button = dfirstChild; //parse a list of elementsvar buttons = d;
Polyfills
This library provides also some polyfills to add support for some DOM manipulation convenience methods missing in Explorer and Edge:
Element.prototype.matches
Element.prototype.closest
Element.prototype.remove
Element.prototype.append
Element.prototype.prepend
Element.prototype.before
Element.prototype.after
Element.prototype.replaceWith
NodeList.prototype.forEach
:scope
selector