a small, simple, and fast DOM helper library
A few commonly used dom traverstal and event binding techniques are not easy to write in pure DOM, this provides a simple way to do them.
doc can be used very much like jQuery:
var doc = require('doc-js');
doc(target); // NodeList
where 'target' can be a CSS selector, a HTMLElement, or a list of elements, eg:
doc('.things'); // List of nodes that have the class 'things'
There are two versions of every function; a legacy way, and a fluent way.
Legacy way example:
doc.is(target, selector);
Fluent way example:
doc(target).is(selector)();
Note that the fluent way will build a list of opperations to perform, and won't execute them untill you call the returned function:
var thingsToDo = doc(target).addClass('things').is('.things');
// nothing touched yet...
// execute the opperations.
thingsToDo();
less than 1k minified
about 1.3k min'd and gzipped (I added features..)
http://jsperf.com/closest-element
MIT
finds elements that match the query within the scope of target
//fluent
doc(target).find(query)();
//legacy
doc.find(target, query);
finds the first element that matches the query within the scope of target
//fluent
doc(target).findOne(query)();
//legacy
doc.findOne(target, query);
recurses up the DOM from the target node, checking if the current element matches the query
//fluent
doc(target).closest(query)();
//legacy
doc.closest(target, query);
returns true if the target element matches the query
//fluent
doc(target).is(query)();
//legacy
doc.is(target, query);
adds classes to the target
//fluent
doc(target).addClass(query)();
//legacy
doc.addClass(target, query);
removes classes from the target
//fluent
doc(target).removeClass(query)();
//legacy
doc.removeClass(target, query);
binds a callback to a target when a DOM event is raised.
//fluent
doc(target/proxy).on(events, target[optional], callback)();
note: if a target is passed to the .on function, doc's target will be used as the proxy.
//legacy
doc.on(events, target, query, proxy[optional]);
removes events assigned to a target.
//fluent
doc(target/proxy).off(events, target[optional], callback)();
note: if a target is passed to the .on function, doc's target will be used as the proxy.
//legacy
doc.off(events, target, callback, proxy);
adds elements to a target
//fluent
doc(target).append(children);
//legacy
doc.append(target, children);
adds elements to the front of a target
//fluent
doc(target).prepend(children);
//legacy
doc.prepend(target, children);