Pluggable DOM selector
var dom = require('dom').use({
html: require('dom.html'),
css: require('dom.css'),
on: require('dom.on')
});
var body = dom('body')
.css({ backgroundColor: 'red', color: 'white' })
.html('<h1>Click me!</h1>');
dom('h1').on('click', function () {
body.css({ backgroundColor: 'blue' });
});dom uses querySelectorAll internally
dom uses mujs plug as a plugin system
with null selectors dom plugins are not called but they keep returning the
chain preventing TypeError exceptions
dom('#nonexistent').foo().bar().qux();Assuming foo, bar and qux are loaded plugins, the previous code does
nothing but don't crash
dom accepts a HTML string as input instead of a selector
HTML strings are detected if its 1st character is <
In such cases, a new document fragment is created and filled with the provided markup. The fragment is not attached to the DOM tree
dom selections are also supported as selectors
domo can receive multiple selectors
Initial selection will be filtered through the following selectors
Only CSS selectors can be used as filters
each provides direct access to selected nodes
dom('button').each(function (node) {
if (node.click) { node.click(); }
});dom is packaged with plugins covering the most common tasks
chain-breaker getter / chainable setter
Replace HTML from selected elements
chainable setter
Remove all children of selected elements
chainable setter
Append HTML or DOM nodes to selected elements
<div id="title"></div>
<ul id="list">
<li class="item">Item</i>
</ul>dom('#title').append('<h1>Title</h1>');
dom('#list').append(dom('#list.item').clone());chain-breaker getter / chainable setter
Returns an object with all HTML attributes of the first selected element
<div class="alert" id="main-alert-bar"></div>dom('.alert').attr();> {
class: 'alert',
id: 'main-alert-bar'
}
Returns the attribute value of the first selected element
dom('.alert').attr('id');> 'main-alert-bar'
Updates the value of the provided attribute in all selected elements
Updates the value of all providede attributes in all selected elements
chain-breaker getter / chainable setter
Alter CSS properties of selected elements
chain-breaker getter
Get an array of classes of the first selected element
chain-breaker getter
Check if a class is in the first selected element
chainable setter
Add a class to selected elements
chainable setter
Remove a class of selected elements
chainable setter
Add or remove a class to selected elements depending on the presence of the class
chain-breaker getter / chain-breaker setter
If an argument is passed it is assigned to the first selected element value
Return the value of the first selected element
<input class="first arg" type="text" />
+ <input class="second arg" type="text" /><br />
= <input id="result" type="text" readonly />var int = function (arg) {
return parseInt(arg, 10) || 0;
};
dom('.arg').on('input', function () {
var first = int(dom('.first.arg').val()),
second = int(dom('.second.arg').val()),
dom('#result').val(first + second);
});chainable setter
Add event listeners to selected elements
chain-breaker getter
Return a clone the first selected node and its children
chainable setter
Remove all selected elements
<ul id="list">
<li>One</li>
<li class="remove">Two</li>
<li>Three</li>
<li class="remove">Four</li>
<li>Five</li>
</ul>dom('#list .remove').remove();