doc

What

a small, simple, and fast DOM helper library

Why?

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.

Usage

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();

Goals

Easy to use

Tiny

less than 1k minified

about 1.3k min'd and gzipped (I added features..)

Fast

http://jsperf.com/closest-element

License

MIT

.find

finds elements that match the query within the scope of target

//fluent

doc(target).find(query)();



//legacy

doc.find(target, query);

.findOne

finds the first element that matches the query within the scope of target

//fluent

doc(target).findOne(query)();



//legacy

doc.findOne(target, query);

.closest

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);

.is

returns true if the target element matches the query

//fluent

doc(target).is(query)();



//legacy

doc.is(target, query);

.addClass

adds classes to the target

//fluent

doc(target).addClass(query)();



//legacy

doc.addClass(target, query);

.removeClass

removes classes from the target

//fluent

doc(target).removeClass(query)();



//legacy

doc.removeClass(target, query);

.on

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]);

.off

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);

.append

adds elements to a target

//fluent

doc(target).append(children);



//legacy

doc.append(target, children);

.prepend

adds elements to the front of a target

//fluent

doc(target).prepend(children);



//legacy

doc.prepend(target, children);

.isVisible

checks if an element or any of its parents display properties are set to 'none'

//fluent

doc(target).isVisible();



//legacy

doc.isVisible(target);

.addFunction

Used to add functionality to doc. This is needed because of the way doc's fluent functions queue.

doc.addFunction('myFunc', myFunc, fluentMyFunc);

the fluentFunction propery only needs to be passed if needed, examples are .on and .off.