MOD is a super slim & lightweight jQuery replacement. It aims to have a very similar & familiar syntax but does differ in many ways.
MOD hopes to have solved the following 'problems':
- jQuery is close to 100kb minified. Version 2, which drops lots of legacy support, is (almost impressively) unreduced in size.
- Jumping between pure DOM Elements and jQuery objects is a true pain. It should simply not be a part of the workflow of modern web development.
- When working with Angular you only get access to jqLite minus selectors (and selectors is a pretty import to us).
So yeah cant say much more than that.
MOD solves problem number 2 (above) by doing what you dont like; namely modifying the built-in Node, Document, Element, EventTarget and NodeList prototypes. This creates a much more natural workflow than jQuery or similar. No more need to remember whether the variable you are working with is an element, or a jQuery object. Now it always has the capabilities of both.
MOD seems to work in all modern web browsers. Preliminary tests indicate full functionality in IE9, Firefox 3.6, Chrome 3, Safari 4 and Opera 9 and later. Shitloads of testing left though.
Currently MOD is, like jQuery, chainable. However, traverser methods (e.g. next()
)
will return null
if no match is found. In such a situation one would get an
error when calling next().css()
. To avoid this one would have to do like so:
var next = node.next();
if(next) next.css('unicode-bidi', 'bidi-override');
When working with NodeLists (see below) instead of Nodes, one will never get this error since all traverser methods return empty NodeLists if no matching nodes are found.
Unfortunately we have not had time to test the performance of MOD against other libraries. But since it always uses native functions, it should be super quick.
If you want to contribute, send a mail to info@thecodebureau.com.
Right now the main thing needed is testing of performance.
All methods are added to a specific prototype (e.g. Node or EventTarget),
but also to the NodeList prototype. All methods are thus also available
on instances of NodeLists. Certain properties (such as Element.children) are
actually HTMLCollections, not NodeLists. Since HTMLCollections are currently
not extended, do something like M(element.children)
to get a (non-live)
NodeList. They will probably not be extended either, since they are live lists.
And iterating through and manipulating live lists is very tricky.
If a method returns Nodes or NodeLists, the method will return a NodeList when called on a NodeList. If a method returns strings, a concatted string will be returned. If a method returns a boolean (only hasClass), a boolean will be returned.
Traversers are methods that traverses the DOM and returns null, a Node or a NodeList.
- childs
- descendants
- closest
- first
- last
- next
- parent
- prev
Manipulators are methods that inserts or removes nodes and returns the current node.
- after
- append
- before
- prepend
- replaceWith
- vanish
- wrap
These methods either update a property and returns the current node or returns a string.
- css
- html
- outerHtml
- text
- clone
Instead of just $()
, MOD uses M()
, O()
and D()
. M as in Many, O as in One
and DOM as in D. Read on to see the differences between them...
This is the function MOD uses to extend the prototypes. func
will be
added to the passed prototype or prototypes. If attachToNodeList
is
true it will also add the function to the NodeList prototype so you
can call the function on all nodes in a NodeList.
M as in Many. Mostly same as O(ufo)
but always returns a NodeList.
Returns null
if unclassified ufo
is passed.
- M(htmlString)
- Returns a
NodeList
of all nodes created from the valid HTML stringhtmlString
(by calling document.createFromHtml(htmlString)). - M(selector)
- Alias for
Document.prototype.M()
which is alias forDocument.prototype.querySelectorAll()
. - M(node)
- Returns a
NodeList
only containingnode
. - M(array || nodeList || htmlCollection)
- Returns collection as a (non-live)
NodeList
.
O as in One. Mostly same as M(ufo)
but always returns a single node. Returns
null
if unclassified ufo
is passed.
- O(htmlString)
- Returns the first (or only) node after creating DOM Nodes from
htmlString
(by calling document.createFromHtml(htmlString)). - O(selector)
- Alias for
Document.prototype.O()
which is alias forDocument.prototype.querySelector()
. - O(array || nodeList || htmlCollection)
- Returns the first node in the collection.
Does the same as $(document).ready(func)
.
M as in Many. Alias for querySelectorAll()
. Returns a NodeList.
O as in One. Alias for querySelector()
. Returns an Element.
The method sets the innerHTML
property of a fictional DIV element to
htmlString
. It then turns all of that elements child nodes (even text nodes
if that's what the htmlString
created) into a non-live NodeList and returns
it.
Creates an event and binds the this
value in listener
to that of thisArg
. If params
(should be an array) is provided then those will
be passed to listener
after the event object.
Not implemented yet.
Same as bind()
but without the possibility of binding
the this
parameter.
Same as on()
except the listener is removed after it is
called the first time.
Same as bind()
except the listener is removed after it is
called the first time.
Inserts node
after the current node.
Recursive closest().
- append(node)
- Inserts
node
as last child of the current node. - append(nodeList)
- Inserts the nodes
nodeList
at the end of the current node.
- before(node)
- Inserts
node
before the current node. - before(nodeList)
- Inserts the nodes in
nodeList
before the current node.
Returns a clone of the current node. The clone is shallow unless isDeep
is
true
.
Traverses the DOM upwards until an element that matches
selector
is found. If no selector is passed it returns this.papa()
.
If stop
is provided, closest will iterate up through the DOM until stop
is encountered. Without stop
it will iterate until it reaces the document
.
Returns descendant nodes to the current node. If levels
is set it only
returns a set amount of levels (e.g. to only get children, call with 1 level).
Ignores text nodes that only contain whitespaces and are adjacent to block Elements.
- descendants(null || undefined || 1, levels)
- Returns (as a NodeList) all descendant nodes that are instance of Element.
- descendants(nodeType, levels)
- Returns (as a NodeList) all descendant nodes that are of
nodeType
. If nodeType === 0, then it returns all descendants. - descendants(selector, levels)
- Returns all descendant nodes that matches the
selector
. If all levels are wanted thenM(selector)
would be a better option.
Calls func
with this
parameter set to current node. Mainly
used for NodeLists.
Remove all child nodes of the current node.
Retrieves the first child node according to criterias in ufo
.
If dontIterate
is true, then it will only check the very
first child node. Otherwise returns the first child node that matches
the criteria, or null if none.
Ignores text nodes that only contain whitespaces and are adjacent to block Elements.
- first(dontIterate)
- Returns first child that is an Element.
- first(nodeType, dontIterate)
- Return first of
nodeType
. IfnodeType === 0
, then it returns the first node of any type. - first(selector, dontIterate)
- Return first that matches
selector
.
Returns innerHTML
or textContent
, or sets innerHTML
depending on ufo
.
This method is attached to Node.prototype
so that instances of
Document, DocumentFragment and Element all get access to it.
- html()
- Returns
this.innerHTML
if possible, otherwisethis.textContent
. - html(htmlString)
- Sets innerHTML if possible.
Opposite of first()
. Otherwise identical.
Calls descendants(ufo, 1)
.
- offspring()
- Returns (as a NodeList) all child nodes that are instance of Element.
- offspring(true)
- Returns (as a NodeList) all child nodes (both Nodes & Elements).
- offspring(nodeType)
- Returns (as a NodeList) all child nodes that are of
nodeType
. - offspring(selector)
- Returns (as a NodeList) all child nodes that match
selector
.
Same as html()
, but affects the outerHTML
property.
NOTE: Will not work for Elements not in the 'visible' DOM because they have no parents. Unless they are present in a DocumentFragment.
Retrieves the next sibling node according to criterias in ufo
.
If dontIterate
is true
, then it will only check the very
first sibling. Otherwise it iterates first of the following
sibling nodess that match the criteria, or null if none.
Ignores text nodes that only contain whitespaces and are adjacent to block Elements.
- next(dontIterate)
- Returns the next node that is an Element.
- next(nodeType, dontIterate)
- Return next of
nodeType
. IfnodeType === 0
, then it retrieves the next of any type. - next(selector, dontIterate)
- Return next that matches
selector
.
Returns the parent node. If selector
is present it only returns it if it
matches it; otherwise null
.
Identical to append()
, but inserts ufo
at the beginning of the current node.
Same as next()
but the opposite.
Removes the current node and all of its children from the DOM.
Replaces the current node and all of its children with node
.
Sets or returns this.textContent
depending on whether
textString
is a string.
Wraps the current node with an element generated from ufo
. The wrapping
is done by inserting the new element before the current node, and then appending
the node to the element.
- wrap(element)
- Wraps the current node by creating an element with `element.clone()` (shallow).
- wrap(htmlString)
- Wraps the current node by creating an element with `O(htmlString)`.
Element.prototype.matches = Element.prototype.matches || Element.prototype.matchesSelector ||
Element.prototype.msMatchesSelector || Element.prototype.mozMatchesSelector ||
p.webkitMatchesSelector || p.oMatchesSelector;
Same as for Document.prototype.M()
.
Same as for Document.prototype.M()
.
Takes a space seperated string of classes and adds them if they are not already present.
Append prefix
to all attribute names of the current element. Note that to
return an attribute using the prefix one must call attr(attributeName, undefined, prefix)
.
- attr(attributeName)
- Return the attribute
attributeName
. - attr(attributeName, null)
- Remove the attribute
attributeName
- attr(attributeName, value)
- Set the attribute
attributeName
tovalue
. - attr(attributes)
-
attributes
is an object of attribute-value pairs to set. If the value of the pair is set tonull
, then the attribute will be removed.
- css(styleName)
- Return the value of `styleName`.
- css(styleName, value)
- Set `styleName` to `value`.
- css(styles)
- `styles` is an object of style-value pairs to set.
Basically just does the same thing as attr()
, but with the prefix
parameter
always set to 'data'.
Returns wheter any of the space separated classes in classString
are present on
the current element.
Not implemented yet.
Remove all of the space separated classes in classString
if present on the
current element.
Toggle all of the space separated classes in classString.
Returns the current NodeList as an array.
Returns indexOf node
in current NodeList.
- includes(node)
- Check if the current NodeList contains
node
. - includes(selector)
- Checks if any of the nodes in the current NodeList matches
selector
.