Socket
Socket
Sign inDemoInstall

d_js

Package Overview
Dependencies
0
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    d_js

DOM manipulation micro-library


Version published
Maintainers
1
Created

Changelog

Source

1.6.0 - 2017-02-24

Changed

  • Removed the second argument of d.get and d.getAll (to select the context). Now you have to use a plain object. For example: d.get({'li': document}). This allows to use context in other functions like d.on, d.insertAfterTo, etc...

Readme

Source

d.js

Micro dom manipulation library. Because jQuery is not needed always.

A lot of this code is taken from http://youmightnotneedjquery.com/

  • Compatible with modern browsers and IE >= 10
  • Installable with bower bower install d.js
  • Compatible with AMD, commonJS and global javascript
  • Only 4Kb (minified)
  • HTML & SVG support

Usage example:

//Get an array with all .buttons elements
var buttons = d.getAll('.buttons');

//Change css properties
d.css(buttons, {
	fontFamily: 'Arial',
	color: 'red',
	transition: 'all 2s'
});

//Handle events
d.on('click', buttons, function () {
	alert('clicked');
});

API

d.get(query)

Returns the first element found:

  • query A string with the selector, array of elements, an object or a Node/NodeList/HTMLCollection instance
var container = d.get('.container');

//Use an object to specify the context
var buttonInContainer = d.get({'.button': container});

d.getAll(query)

Returns an array with all elements found:

  • query A string with the selector, array of elements, an object or a Node/NodeList/HTMLCollection instance
d.get('.button').forEach(function (el) {
	el.classList.add('selected');
});

d.is(element, query)

Returns if the element matches with the selector:

  • element The element
  • query A string with the selector
d.is(document.body, 'h1'); //false
d.is(document.body, 'body'); //true

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, array of elements or a Node/NodeList/HTMLCollection instance
  • callback The event callback
  • useCapture (optional)
function clickAction(e) {
	alert('Event ' + e.type);
}

d.on('click', '.button', clickAction);

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, array of elements or a Node/NodeList/HTMLCollection instance
  • target A string with the target selector
  • callback The event callback
  • useCapture (optional)
function clickAction(e) {
	alert('Event ' + e.type);
}

d.on('click', '.navigation', 'a', clickAction);

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, array of elements or a Node/NodeList/HTMLCollection instance
  • callback The event callback
  • useCapture (optional)
d.off('click', '.button', clickAction);

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, array of elements or a Node/NodeList/HTMLCollection instance
d.trigger('click', '.button');

d.remove(query)

Removes the elements from the DOM

  • query A string with the selector, array of elements or a Node/NodeList/HTMLCollection instance
d.remove('.button');

d.insertAfter(query, content)

Insert new elements after other

  • query A string with the selector, array of elements or a NodeList/HTMLCollection instance
  • content A string with the selector or html content, array of elements or a Node/NodeList/HTMLCollection instance
d.insertAfter('li:last-child', newNodes);
d.insertAfter('li:last-child', '<li>new content</li>');

d.insertBefore(query, content)

Insert new elements before other

  • query A string with the selector, array of elements or a NodeList/HTMLCollection instance
  • content A string with the selector or html content, array of elements or a Node/NodeList/HTMLCollection instance
d.insertBefore('li:first-child', newNodes);
d.insertBefore('li:first-child', '<li>new content</li>');

d.prepend(query, content)

Insert new elements as first children of other element

  • query A string with the selector, array of elements or a NodeList/HTMLCollection instance
  • content A string with the selector or html content, array of elements or a Node/NodeList/HTMLCollection instance
d.prepend('ul', newLiNode);
d.prepend('ul', '<li>new content</li>');

d.append(query, content)

Insert new elements as last children of other element

  • query A string with the selector, array of elements or a NodeList/HTMLCollection instance
  • content A string with the selector or html content, array of elements or a Node/NodeList/HTMLCollection instance
d.append('ul', newLiNode);
d.append('ul', '<li>new content</li>');

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 value
var color = d.css('.button', 'color');

//Set a new value
d.css('.button', 'color', 'blue');

//Set several values
d.css('.button', {
	color: 'red',
	backgroundColor: ['blue', 'red', 'green'], //set different values for each element
	transform: 'rotate(5deg)' //don't care about vendor prefixes
	width: function (el, index) { //use a function to calculate the value for each element
		return (100 + (100*index)) + 'px';
	}
});

d.parse()

Parses html code. Returns an element or an array of elements

  • html A string with the code to parse
  • forceArray To return an array of elements even if just one element has been parsed
//parse one element
var button = d.parse('<button>Hello</button>');
button.classList.add('active');

//parse a list of elements
var buttons = d.parse('<button>Hello</button><button>World</button>');

buttons.forEach(function (el) {
	el.classList.add('active');
});

Chaining

d.js allows to create d instances so you can chain some of these methods. Example:

d('.button')
	.css({
		color: 'red',
		fontFamily: 'Arial'
	}).
	.on('click', function (e) {
		alert('Button clicked');
	})
	.append('.buttons');

//You can create new elements on the fly:
d('<button>Click me</button>')
	.css({
		color: 'red',
		fontFamily: 'Arial'
	})
	.on('click', function () {
		alert('Hi!');
	})
	.appendTo('.buttons');

Chainable methods:

MethodDescription
.on(event, callback, useCapture)Attach an event.
.off(event, callback, useCapture)Removes an event.
.delegate(event, target, callback, useCapture)Delegates an event.
.trigger(event, data)Trigger an event
`.css(propsprop, value)`
`.insertBefore(contentquery)`
`.insertAfter(contentquery)`
`.prepend(contentquery)`
`.append(contentquery)`
.insertBeforeTo(query)Insert the element before other element
.insertAfterTo(query)Insert the element after other element
.prependTo(query)Insert the element as first child of other element
.appendTo(query)Insert the element as last child of other element

FAQs

Last updated on 24 Feb 2017

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc