Cash

An absurdly small jQuery alternative for modern browsers.

README


Cash Logo


Cash


Cash is an absurdly small jQuery alternative for modern browsers (IE11+) that provides jQuery-style syntax for manipulating the DOM. Utilizing modern browser features to minimize the codebase, developers can use the familiar chainable methods at a fraction of the file size. 100% feature parity with jQuery isn't a goal, but Cash comes helpfully close, covering most day to day use cases.

Comparison


SizeCashZeptojQuery
---------------------------------------------------------
Unminified**36.558.7227
Minified**162671
Minified**69.824.4

A 76.6% gain in size reduction compared to jQuery Slim. If you need a smaller bundle, we support partial builds too.

FeaturesCashZeptojQuery
----------------------------------------------------------------------------------------------------------------------
Supports️❌
Supports️✔
Actively
Namespaced️❌
Typed️❌
TypeScript⚠️⚠️
Partial⚠️⚠️

If you're migrating from jQuery be sure to read our migration guide.

Usage


Get Cash from CloudFlare or jsDelivr and use it like this:

  1. ``` html
  2. <script src="https://cdnjs.cloudflare.com/ajax/libs/cash/8.1.2/cash.min.js"></script>
  3. <script>
  4.   $(function () {
  5.     $('html').addClass ( 'dom-loaded' );
  6.     $('<footer>Appended with Cash</footer>').appendTo ( document.body );
  7.   });
  8. </script>
  9. ```

Cash is also available through npm as the [cash-dom](https://www.npmjs.com/package/cash-dom) package:

  1. ```sh
  2. npm install --save cash-dom
  3. ```

That you can then use like this:

  1. ``` js
  2. import $ from "cash-dom";

  3. $(function () {
  4.   $('html').addClass ( 'dom-loaded' );
  5.   $('<footer>Appended with Cash</footer>').appendTo ( document.body );
  6. });
  7. ```

Documentation


Cash gives you a query selector, collection methods and some library methods. If you need more details about our API just check out jQuery's, while we don't implement everything that jQuery provides, pretty much everything that we do implement should be compatible with jQuery. Cash can be extended with custom methods, read how here.

$()


This is the main selector method for Cash. It returns an actionable collection of nodes.

If a function is provided, the function will be run once the DOM is ready.

  1. ``` js
  2. $( selector [, element] ) // => collection, using `element` as the context
  3. $( selector [, collection] ) // => collection, using `collection` as the context
  4. $(node) // => collection
  5. $(nodeList) // => collection
  6. $(htmlString) // => collection
  7. $(collection) // => self
  8. $(function () {}) // => document ready callback
  9. ```

Collection Methods


These methods from the collection prototype ($.fn) are available once you create a collection with$() and are called like so:

  1. ``` js
  2. $(element).addClass ( className ) // => collection
  3. ```

Some extra methods are available but disabled by default.

AttributesCollectionCSSDataDimensionsEffects
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
[fn.addClass[fn.add[fn.css[fn.data[fn.height[fn.hide
[fn.attr[fn.each|[fn.innerHeight[fn.show
[fn.hasClass[fn.eq|[fn.innerWidth[fn.toggle
[fn.prop[fn.filter|[fn.outerHeight|
[fn.removeAttr[fn.first|[fn.outerWidth|
[fn.removeClass[fn.get|[fn.width|
[fn.removeProp[fn.index||
[fn.toggleClass[fn.last||
|||
|||

EventsFormsManipulationOffsetTraversal
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[fn.off[fn.serialize[fn.after[fn.offset[fn.children
[fn.on[fn.val[fn.append[fn.offsetParent[fn.closest
[fn.one|[fn.position[fn.contents
[fn.ready||
[fn.trigger||
|[fn.detach|
|[fn.empty|
|[fn.html|
|[fn.insertAfter|
|[fn.insertBefore|
|[fn.prepend|
|[fn.prependTo|
|[fn.remove|
|[fn.replaceAll|
|[fn.replaceWith|
|[fn.text|
|[fn.unwrap|
|[fn.wrap|
|[fn.wrapAll|
|[fn.wrapInner|

$.fn


The main prototype for collections, allowing you to extend Cash with plugins by adding methods to all collections.

  1. ``` js
  2. $.fn // => Cash.prototype
  3. $.fn.myMethod = function () {}; // Custom method added to all collections
  4. $.fn.extend ( object ); // Add multiple methods to the prototype
  5. ```

fn.add ()


Returns a new collection with the element(s) added to the end.

  1. ``` js
  2. $(element).add ( element ) // => collection
  3. $(element).add ( selector ) // => collection
  4. $(element).add ( collection ) // => collection
  5. ```

fn.addClass ()


Adds the className class to each element in the collection.

Accepts space-separated className for adding multiple classes.

  1. ``` js
  2. $(element).addClass ( className ) // => collection
  3. ```

fn.after ()


Inserts content or elements after the collection.

  1. ``` js
  2. $(element).after ( element ) // => collection
  3. $(element).after ( htmlString ) // => collection
  4. $(element).after ( content [, content] ) // => collection
  5. ```

fn.append ()


Appends content or elements to each element in the collection.

  1. ``` js
  2. $(element).append ( element ) // => collection
  3. $(element).append ( htmlString ) // => collection
  4. $(element).append ( content [, content] ) // => collection
  5. ```

fn.appendTo ()


Adds the elements in the collection to the target element(s).

  1. ``` js
  2. $(element).appendTo ( element ) // => collection
  3. ```

fn.attr ()


Without attrValue, returns the attribute value of the first element in the collection.

With attrValue, sets the attribute value of each element of the collection.

  1. ``` js
  2. $(element).attr ( attrName ) // value
  3. $(element).attr ( attrName, attrValue ) // => collection
  4. $(element).attr ( object ) // => collection
  5. ```

fn.before ()


Inserts content or elements before the collection.

  1. ``` js
  2. $(element).before ( element ) // => collection
  3. $(element).before ( htmlString ) // => collection
  4. $(element).before ( content [, content] ) // => collection
  5. ```

fn.children ()


Without a selector specified, returns a collection of child elements.

With a selector, returns child elements that match the selector.

  1. ``` js
  2. $(element).children () // => collection
  3. $(element).children ( selector ) // => collection
  4. ```

fn.closest ()


Returns the closest matching selector up the DOM tree.

  1. ``` js
  2. $(element).closest ( selector ) // => collection
  3. ```

fn.contents ()


Get the children of each element in the set of matched elements, including text and comment nodes.

Useful for selecting elements in friendly iframes.

  1. ``` js
  2. $('iframe').contents ().find ( '*' ) // => collection
  3. ```

fn.clone ()


Returns a collection with cloned elements.

  1. ``` js
  2. $(element).clone () // => collection
  3. ```

fn.detach ()


Removes collection elements, optionally that match the selector, from the DOM.

  1. ``` js
  2. $(element).detach () // => collection
  3. $(element).detach ( selector ) // => collection
  4. ```

fn.css ()


Returns a CSS property value when just property is supplied.

Sets a CSS property when property and value are supplied.

Sets multiple properties when an object is supplied.

Properties will be autoprefixed if needed for the user's browser.

  1. ``` js
  2. $(element).css ( property ) // => value
  3. $(element).css ( property, value ) // => collection
  4. $(element).css ( object ) // => collection
  5. ```

fn.data ()


Without arguments, returns an object mapping all the data-* attributes to their values.

With a key, return the value of the corresponding data-* attribute.

With both a key and value, sets the value of the corresponding data-* attribute to value.

Multiple data can be set when an object is supplied.

  1. ``` js
  2. $(element).data () // => object
  3. $(element).data ( key ) // => value
  4. $(element).data ( key, value ) // => collection
  5. $(element).data ( object ) // => collection
  6. ```

fn.each ()


Iterates over a collection with callback ( index, element ). The callback function may exit iteration early by returning false.

  1. ``` js
  2. $(element).each ( callback ) // => collection
  3. ```

fn.empty ()


Empties the elements interior markup.

  1. ``` js
  2. $(element).empty () // => collection
  3. ```

fn.eq ()


Returns a collection with the element at index.

  1. ``` js
  2. $(element).eq ( index ) // => collection
  3. ```

fn.extend ()


Adds properties to the Cash collection prototype.

  1. ``` js
  2. $.fn.extend ( object ) // => object
  3. ```

fn.filter ()


Returns the collection that results from applying the filter selector/method.

  1. ``` js
  2. $(element).filter ( selector ) // => collection
  3. $(element).filter ( function ( index, element ) {} ) // => collection
  4. ```

fn.find ()


Returns selector match descendants from the first element in the collection.

  1. ``` js
  2. $(element).find ( selector ) // => collection
  3. ```

fn.first ()


Returns a collection containing only the first element.

  1. ``` js
  2. $(element).first () // => collection
  3. ```

fn.get ()


Returns the element at the index, or returns all elements.

  1. ``` js
  2. $(element).get ( index ) // => domNode
  3. $(element).get () // => domNode[]
  4. ```

fn.has ()


Reduce the set of matched elements to those that have a descendant that matches the selector or DOM element.

  1. ``` js
  2. $(element).has ( selector ) // => collection
  3. $(element).has ( element ) // => collection
  4. ```

fn.hasClass ()


Returns the boolean result of checking if any element in the collection has the className attribute.

  1. ``` js
  2. $(element).hasClass ( className ) // => boolean
  3. ```

fn.height ()


Returns or sets the height of the element.

  1. ``` js
  2. $(element).height () // => Integer
  3. $(element).height ( number ) // => collection
  4. ```

fn.hide ()


Hide the elements.

  1. ``` js
  2. $(element).hide () // => collection
  3. ```

fn.html ()


Returns the HTML text of the first element in the collection, sets the HTML if provided.

  1. ``` js
  2. $(element).html () // => HTML Text
  3. $(element).html ( htmlString ) // => HTML Text
  4. ```

fn.index ()


Returns the index of the element in its parent if an element or selector isn't provided. Returns index within element or selector if it is.

  1. ``` js
  2. $(element).index () // => Integer
  3. $(element).index ( element ) // => Integer
  4. ```

fn.innerHeight ()


Returns the height of the element + padding.

  1. ``` js
  2. $(element).innerHeight () // => Integer
  3. ```

fn.innerWidth ()


Returns the width of the element + padding.

  1. ``` js
  2. $(element).innerWidth () // => Integer
  3. ```

fn.insertAfter ()


Inserts collection after specified element.

  1. ``` js
  2. $(element).insertAfter ( element ) // => collection
  3. ```

fn.insertBefore ()


Inserts collection before specified element.

  1. ``` js
  2. $(element).insertBefore ( element ) // => collection
  3. ```

fn.is ()


Returns whether the provided selector, element or collection matches any element in the collection.

  1. ``` js
  2. $(element).is ( selector ) // => boolean
  3. ```

fn.last ()


Returns a collection containing only the last element.

  1. ``` js
  2. $(element).last () // => collection
  3. ```

fn.map ()


Returns a new collection, mapping each element with callback ( index, element ).

  1. ``` js
  2. $(selector).map ( callback ) // => collection
  3. ```

fn.next ()


Returns the next adjacent elements.

  1. ``` js
  2. $(element).next () // => collection
  3. $(element).next ( selector ) // => collection
  4. ```

fn.nextAll ()


Returns all the next elements.

  1. ``` js
  2. $(element).nextAll () // => collection
  3. $(element).nextAll ( selector ) // => collection
  4. ```

fn.nextUntil ()


Returns all the next elements, until the provided selector matches.

  1. ``` js
  2. $(element).nextUntil ( selector ) // => collection
  3. $(element).nextUntil ( selector, filterSelector ) // => collection
  4. ```

fn.not ()


Filters collection by false match on collection/selector.

  1. ``` js
  2. $(element).not ( selector ) // => collection
  3. $(element).not ( collection ) // => collection
  4. ```

fn.off ()


Removes event listener from collection elements.

Accepts space-separated eventName for removing multiple events listeners.

Removes all event listeners if called without arguments.

  1. ``` js
  2. $(element).off ( eventName, eventHandler ) // => collection
  3. $(element).off ( eventName ) // => collection
  4. $(element).off ( eventsMap ) // => collection
  5. $(element).off () // => collection
  6. ```

fn.offset ()


Get the coordinates of the first element in a collection relative to the document.

  1. ``` js
  2. $(element).offset () // => Object
  3. ```

fn.offsetParent ()


Get the first element's ancestor that's positioned.

  1. ``` js
  2. $(element).offsetParent () // => collection
  3. ```

fn.on ()


Adds event listener to collection elements.

Accepts space-separated eventName for listening to multiple events.

Event is delegated if delegate is supplied.

  1. ``` js
  2. $(element).on ( eventsMap ) // => collection
  3. $(element).on ( eventName, eventHandler ) // => collection
  4. $(element).on ( eventName, delegate, eventHandler ) // => collection
  5. ```

fn.one ()


Adds event listener to collection elements that only triggers once for each element.

Accepts space-separated eventName for listening to multiple events.

Event is delegated if delegate is supplied.

  1. ``` js
  2. $(element).one ( eventName, eventHandler ) // => collection
  3. $(element).one ( eventName, delegate, eventHandler ) // => collection
  4. ```

fn.outerHeight ()


Returns the outer height of the element. Includes margins if includeMargings is set to true.

  1. ``` js
  2. $(element).outerHeight () // => Integer
  3. $(element).outerHeight ( includeMargins ) // => Integer
  4. ```

fn.outerWidth ()


Returns the outer width of the element. Includes margins if includeMargings is set to true.

  1. ``` js
  2. $(element).outerWidth () // => Integer
  3. $(element).outerWidth ( includeMargins ) // => Integer
  4. ```

fn.parent ()


Returns collection of elements who are parent of elements.

  1. ``` js
  2. $(element).parent () // => collection
  3. $(element).parent ( selector ) // => collection
  4. ```

fn.parents ()


Returns collection of elements who are parents of elements. Optionally filtering by selector.

  1. ``` js
  2. $(element).parents () // => collection
  3. $(element).parents ( selector ) // => collection
  4. ```

fn.parentsUntil ()


Returns collection of elements who are parents of elements, until a provided selector matches. Optionally filtering by selector.

  1. ``` js
  2. $(element).parentsUntil ( selector ) // => collection
  3. $(element).parentsUntil ( selector, filterSelector ) // => collection
  4. ```

fn.position ()


Get the coordinates of the first element in a collection relative to its offsetParent.

  1. ``` js
  2. $(element).position () // => object
  3. ```

fn.prepend ()


Prepends content or elements to the each element in collection.

  1. ``` js
  2. $(element).prepend ( element ) // => collection
  3. $(element).prepend ( htmlString ) // => collection
  4. $(element).prepend ( content [, content] ) // => collection
  5. ```

fn.prependTo ()


Prepends elements in a collection to the target element(s).

  1. ``` js
  2. $(element).prependTo ( element ) // => collection
  3. ```

fn.prev ()


Returns the previous adjacent elements.

  1. ``` js
  2. $(element).prev () // => collection
  3. $(element).prev ( selector ) // => collection
  4. ```

fn.prevAll ()


Returns all the previous elements.

  1. ``` js
  2. $(element).prevAll () // => collection
  3. $(element).prevAll ( selector ) // => collection
  4. ```

fn.prevUntil ()


Returns all the previous elements, until the provided selector matches.

  1. ``` js
  2. $(element).prevUntil ( selector ) // => collection
  3. $(element).prevUntil ( selector, filterSelector ) // => collection
  4. ```

fn.prop ()


Returns a property value when just property is supplied.

Sets a property when property and value are supplied, and sets multiple properties when an object is supplied.

  1. ``` js
  2. $(element).prop ( property ) // => property value
  3. $(element).prop ( property, value ) // => collection
  4. $(element).prop ( object ) // => collection
  5. ```

fn.ready ()


Calls callback method on DOMContentLoaded.

  1. ``` js
  2. $(document).ready ( callback ) // => collection/span
  3. ```

fn.remove ()


Removes collection elements, optionally that match the selector, from the DOM and removes all their event listeners.

  1. ``` js
  2. $(element).remove () // => collection
  3. $(element).remove ( selector ) // => collection
  4. ```

fn.replaceAll ()


This is similar to fn.replaceWith (), but with the source and target reversed.

  1. ``` js
  2. $(element).replaceAll ( content ) // => collection
  3. ```

fn.replaceWith ()


Replace collection elements with the provided new content.

  1. ``` js
  2. $(element).replaceWith ( content ) // => collection
  3. ```

fn.removeAttr ()


Removes attribute from collection elements.

Accepts space-separated attrName for removing multiple attributes.

  1. ``` js
  2. $(element).removeAttr ( attrName ) // => collection
  3. ```

fn.removeClass ()


Removes className from collection elements.

Accepts space-separated className for adding multiple classes.

Providing no arguments will remove all classes.

  1. ``` js
  2. $(element).removeClass () // => collection
  3. $(element).removeClass ( className ) // => collection
  4. ```

fn.removeProp ()


Removes property from collection elements.

  1. ``` js
  2. $(element).removeProp ( propName ) // => collection
  3. ```

fn.serialize ()


When called on a form, serializes and returns form data.

  1. ``` js
  2. $(form).serialize () // => String
  3. ```

fn.show ()


Show the elements.

  1. ``` js
  2. $(element).show () // => collection
  3. ```

fn.siblings ()


Returns a collection of sibling elements.

  1. ``` js
  2. $(element).siblings () // => collection
  3. $(element).siblings ( selector ) // => collection
  4. ```

fn.slice ()


Returns a new collection with elements taken from start to end.

  1. ``` js
  2. $(selector).slice ( start, end ) // => collection
  3. ```

fn.text ()


Returns the inner text of the first element in the collection, sets the text if textContent is provided.

  1. ``` js
  2. $(element).text () // => text
  3. $(element).text ( textContent ) // => collection
  4. ```

fn.toggle ()


Hide or show the elements.

  1. ``` js
  2. $(element).toggle () // => collection
  3. $(element).toggle ( force ) // => collection
  4. ```

fn.toggleClass ()


Adds or removes className from collection elements based on if the element already has the class.

Accepts space-separated classNames for toggling multiple classes, and an optional force boolean to ensure classes are added (true) or removed (false).

  1. ``` js
  2. $(element).toggleClass ( className ) // => collection
  3. $(element).toggleClass ( className, force ) // => collection
  4. ```

fn.trigger ()


Triggers supplied event on elements in collection. Data can be passed along as the second parameter.

  1. ``` js
  2. $(element).trigger ( eventName ) // => collection
  3. $(element).trigger ( eventObj ) // => collection
  4. $(element).trigger ( eventName, data ) // => collection
  5. $(element).trigger ( eventObj, data ) // => collection
  6. ```

fn.unwrap ()


Removes the wrapper from all elements.

  1. ``` js
  2. $(element).unwrap () // => collection
  3. ```

fn.val ()


Returns an inputs value. If value is supplied, sets all inputs in collection's value to the value argument.

  1. ``` js
  2. $(input).val () // => value
  3. $(input).val ( value ) // => collection
  4. ```

fn.width ()


Returns or sets the width of the element.

  1. ``` js
  2. $(element).width () // => number
  3. $(element).width ( number ) // => collection
  4. ```

fn.wrap ()


Wraps a structure around each element.

  1. ``` js
  2. $(element).wrap ( structure ) // => collection
  3. ```

fn.wrapAll ()


Wraps a structure around all elements.

  1. ``` js
  2. $(element).wrapAll ( structure ) // => collection
  3. ```

fn.wrapInner ()


Wraps a structure around all children.

  1. ``` js
  2. $(element).wrapInner ( structure ) // => collection
  3. ```

Cash Methods


These methods are exported from the global $ object, and are called like so:

  1. ``` js
  2. $.isArray ( arr ) // => boolean
  3. ```

Some extra methods are available but disabled by default.

TypeUtilities
------------------------------------------------------------------
[$.isArray[$.guid](#guid)
[$.isFunction[$.each
[$.isNumeric[$.extend
[$.isPlainObject[$.parseHTML
[$.isWindow[$.unique

$.guid


A unique number.

  1. ``` js
  2. $.guid++ // => number
  3. ```

$.each ()


Iterates through an array and calls the callback ( index, element ) method on each element.

Iterates through an object and calls the callback ( key, value ) method on each property.

The callback function may exit iteration early by returning false.

  1. ``` js
  2. $.each ( array, callback ) // => array
  3. $.each ( object, callback ) // => object
  4. ```

$.extend ()


Extends target object with properties from the source object, potentially deeply too.

  1. ``` js
  2. $.extend ( target, source ) // => object
  3. $.extend ( true, target, source ) // => object
  4. ```

$.isArray ()


Check if the argument is an array.

  1. ``` js
  2. $.isArray ([ 1, 2, 3 ]) // => true
  3. ```

$.isFunction ()


Check if the argument is a function.

  1. ``` js
  2. function fn () {};
  3. $.isFunction ( fn ) // => true
  4. ```

$.isNumeric ()


Check if the argument is numeric.

  1. ``` js
  2. $.isNumeric ( 57 ) // => true
  3. ```

$.isPlainObject ()


Check if the argument is a plain object.

  1. ``` js
  2. $.isPlainObject ( {} ) // => true
  3. ```

$.isWindow ()


Check if the argument is a Window object.

  1. ``` js
  2. $.isWindow ( window ) // => true
  3. ```

$.parseHTML ()


Returns a collection from an HTML string.

  1. ``` js
  2. $.parseHTML ( htmlString ) // => collection
  3. ```

$.unique ()


Returns a new array with duplicates removed.

  1. ``` js
  2. $.unique ( array ) // => array
  3. ```

Contributing


If you found a problem, or have a feature request, please open an issue about it.

If you want to make a pull request you should:

1. Clone the repository: git clone https://github.com/fabiospampinato/cash.git.
2. Enter the cloned repository: cd cash
3. Install the dependencies: npm install.
4. Automatically recompile Cash whenever a change is made: npm run dev.
5. Automatically rerun the tests whenever a change is made: npm run test:watch.
6. Remember to update the readme, if necessary.

Thanks


- @kenwheeler, @shshaw, @jamiebuilds, @simeydotme and all the contributors who helped making Cash.
- @hisk - The "design focused engineer" behind our awesome logo.

License


MIT © Fabio Spampinato