xu.js
A lightweight (2KB!) javascript library to do basic DOM updates, and ajax calls
Usage
Either grab xu
from the dist/
folder in the repo or
npm install --save xu
then either
var xu = require('xu')
(for browserify/webpack users)
or
include xu.js
in your html file
and reference xu
in your javascript
// Gives you the elementvar nav = ; // Can also be accessed dynamically (no variable declaration);
Developing
xu.js uses gulp to build
After making changes in src/xu.js
run
gulp build
And see changes reflected in dist/
Documentation
xu.js is currently in development. Below are a list of all of the (currently) available functions
- css
- setClass
- appendClass
- removeClass
- hasClass
- toggleClass
- attr
- on
- text
- ajax
- get
- post
- each
xu
Returns an array of all elements matching the selector
Usage:
;
Example:
;;;
css
Add styling to an element
Usage:
;
Example:
;
setClass
Override the elements class names with the ones provided
Usage:
;
Example:
;
appendClass
Append to the elements class names (not overriding)
Usage:
;
Example:
;
removeClass
Removes the class specified from the selected element
Usage:
;
Example:
;
hasClass
Returns true or false if the class is present in the xu instance
Usage:
Example:
if /* the element has the class */ else /* ... */
toggleClass
Either adds or removes the specified class depending on if the element has it or not currently
Usage:
;
;
attr
When passed a string, returns the attribute of the element specified
When passed an object, sets the attributes specified on the element
Usage:
; ;
Example:
; //returns src attribute i.e 'img/grass.png' ;
text
Replaces the text of the element, or if called without parameters returns the content of the element(s)
Usage:
// Gettertext; // SettertextnewText: string;
Example:
text'Be careful!'; // returns 'Be careful!'// If there are more than one '.alert' items it will return an array of stringsconsole;
ajax
Basic GET/POST ajax calls. Returns a promise. Called without function parameters on xu (no brackets)
Usage:
xu
Example:
// Default header for POST request is Content-type: application/jsonxu;
get
Shorthand for xu.ajax get request
Usage:
xu
xu ;
post
Shorthand for xu.ajax post request
Default Content-type of application/json
Usage:
xu
xu ;
each
Iterate over arrays/objects with ease
Usage:
xu; xu;
Example:
xu; xu;
xu.prototype.each
Iterate over all elements matching that query. Callback function takes an element parameter that is an instance of xu
so all of xu
's
functions are accessible via that variable.
Usage:
Example:
;