Methods
addClass(el, class)
Add a class to DOM Node.
Parameters:
Name | Type | Description |
---|---|---|
el |
object | DOM Node to add a class to. |
class |
string | The class to add. |
addScript(src, optsopt)
Add a script to the page.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
src |
string | Script src. | |
opts |
object |
<optional> |
Optional listeners for "load" and/or "error". |
adj(node, content, where)
Insert markup relative to a node (insertAdjacentHTML).
Parameters:
Name | Type | Description |
---|---|---|
node |
node | DOM node to insert relative to. |
content |
string | The content to insert. |
where |
string | Where to insert; one of 'before' (beforeBegin), 'front' (afterBegin), 'back' (beforeEnd), or 'after' (afterEnd). |
append(el, child)
Safely append one element to another.
Parameters:
Name | Type | Description |
---|---|---|
el |
HTMLElement | DocumentFragment | Node to append to. |
child |
HTMLElement | DocumentFragment | Node to append. |
args(args) → {array}
Convert `arguments` to an array.
Parameters:
Name | Type | Description |
---|---|---|
args |
object | Arguments object. |
Returns:
Arguments
- Type
- array
bind(fn, objopt, prefillopt, postfillopt) → {function}
Bind a function to a particular scope (`this`).
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
fn |
function | Function to bind. | ||
obj |
object |
<optional> |
window | Scope object (`this` when function executes). |
prefill |
array |
<optional> |
Prefill arguments. | |
postfill |
array |
<optional> |
Postfill arguments. |
Returns:
Bound function.
- Type
- function
buildQuery(object) → {string}
Build a query string from an object containing key/value pairs.
Parameters:
Name | Type | Description |
---|---|---|
object |
object | containing key/value pairs which will be url encoded. |
Returns:
Query params string.
- Type
- string
byClass(class, parentopt) → {array}
Get an array of elements by class name.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
class |
string | Class name(s), space separated. | ||
parent |
object |
<optional> |
document | Optional DOM Node to start from. |
Returns:
Array of elements.
- Type
- array
byId(id) → {object}
Get an element by id.
Parameters:
Name | Type | Description |
---|---|---|
id |
string | Element ID. |
Returns:
DOM Node or null.
- Type
- object
byName(name) → {object}
Get an element by name.
Parameters:
Name | Type | Description |
---|---|---|
name |
string | Node name. |
Returns:
DOM Node or null.
- Type
- object
bySelector(selector, parentopt) → {array}
Get a single element by CSS selector.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
selector |
string | Selector(s), comma separated. | ||
parent |
object |
<optional> |
document | Optional DOM Node to start from. |
Returns:
Element or null.
- Type
- array
bySelectorAll(selector, parentopt) → {array}
Get an array of elements by CSS selector.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
selector |
string | Selector(s), comma separated. | ||
parent |
object |
<optional> |
document | Optional DOM Node to start from. |
Returns:
Array of elements.
- Type
- array
byTag(tag, parentopt) → {array}
Get an array of elements by tag name.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
tag |
string | Tag name. | ||
parent |
object |
<optional> |
document | Optional DOM Node to start from. |
Returns:
An array which will be empty if nothing is found
- Type
- array
cancelFrame(fn)
Cancel animation frame (cancelAnimationFrame).
Parameters:
Name | Type | Description |
---|---|---|
fn |
function | Function to cancel. |
capitalize(str) → {string}
Capitalize a string of text.
Parameters:
Name | Type | Description |
---|---|---|
str |
string | String to capitalize. |
Returns:
Capitalized string.
- Type
- string
clone(obj, shallowopt) → {object}
Clone an object.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
obj |
object | Object to clone. | ||
shallow |
bool |
<optional> |
false | Whether to create a shallow clone. |
Returns:
Cloned object.
- Type
- object
create(el, attrsopt, eventsopt) → {object}
Create a new element.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
el |
string | Tag name. | |
attrs |
object |
<optional> |
Optional attributes passed to setAttrs. |
events |
object |
<optional> |
Optional events passed to setAttrs. |
Returns:
New DOM Node.
- Type
- object
delClass(el, class)
Remove a class from a DOM Node.
Parameters:
Name | Type | Description |
---|---|---|
el |
object | DOM Node to remove a class from. |
class |
string | The class to remove. |
each(obj, fn)
Apply a function to each value in an array or object.
Parameters:
Name | Type | Description |
---|---|---|
obj |
object | array | Object or array to apply to. |
fn |
function | Function to apply which should expect `value`, `index`, and `obj` arguments. |
extend(target, source, methodsopt)
Extend one class with another.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
target |
function | Target class. | |
source |
function | Source class. | |
methods |
object |
<optional> |
Additional methods keyed by method name. |
frag(contentopt) → {object}
Create a document fragment.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
content |
string | node |
<optional> |
Optional html or DOM node to append to the fragment. DOM nodes will be cloned prior to append. |
Returns:
Document fragment.
- Type
- object
get(obj, path, defopt) → {mixed}
Get a value [deep] in an object.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
obj |
object | Object to get a value from. | |
path |
string | array | Property path, e.g. "foo.bar.baz", or ["foo", "bar", "baz"]. | |
def |
mixed |
<optional> |
Default value if nothing is found. |
Returns:
Value or default.
- Type
- mixed
getOffset(el) → {object}
Get the offset position of an element.
Parameters:
Name | Type | Description |
---|---|---|
el |
object | DOM Node to get an offset for. |
Returns:
Object containing "x" and "y" properties.
- Type
- object
getScroll() → {integer}
Get the scroll position of the current window.
Returns:
Scroll position.
- Type
- integer
getStyle(obj, style, defopt) → {string}
Get a CSS property from a DOM Node.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
obj |
object | DOM Node to get a CSS property from. | |
style |
string | CSS property in camelCase. | |
def |
string |
<optional> |
Default value if property is not set. |
Returns:
Property value.
- Type
- string
getVendorPrefix(prop, forCssopt) → {string}
Get the vendor prefix for the current browser.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
prop |
string | CSS property to test. | ||
forCss |
boolean |
<optional> |
false | Return CSS formatting (e.g. "-moz-" instead of "Moz"). |
Returns:
Vendor prefix.
- Type
- string
hasClass(el, class) → {boolean}
Test if a DOM Node has a particular class.
Parameters:
Name | Type | Description |
---|---|---|
el |
object | DOM Node to check. |
class |
string | Class name to check for. |
Returns:
- Type
- boolean
isArray(obj) → {Boolean}
Test for an array.
Parameters:
Name | Type | Description |
---|---|---|
obj |
mixed | Object to test. |
Returns:
- Type
- Boolean
isBool(obj) → {Boolean}
Test for a boolean.
Parameters:
Name | Type | Description |
---|---|---|
obj |
mixed | Object to test. |
Returns:
- Type
- Boolean
isDom(obj) → {Boolean}
Test for a DOM object (HTMLElement or DocumentFragment).
Parameters:
Name | Type | Description |
---|---|---|
obj |
mixed | Object to test. |
Returns:
- Type
- Boolean
isFunc(obj) → {Boolean}
Test for a function.
Parameters:
Name | Type | Description |
---|---|---|
obj |
mixed | Object to test. |
Returns:
- Type
- Boolean
isNull(obj) → {Boolean}
Test for null.
Parameters:
Name | Type | Description |
---|---|---|
obj |
mixed | Object to test. |
Returns:
- Type
- Boolean
isNum(obj) → {Boolean}
Test for a number.
Parameters:
Name | Type | Description |
---|---|---|
obj |
mixed | Object to test. |
Returns:
- Type
- Boolean
isObj(obj) → {Boolean}
Test for an object.
Parameters:
Name | Type | Description |
---|---|---|
obj |
mixed | Object to test. |
Returns:
- Type
- Boolean
isStr(obj) → {Boolean}
Test for a string.
Parameters:
Name | Type | Description |
---|---|---|
obj |
mixed | Object to test. |
Returns:
- Type
- Boolean
isUnd(obj) → {Boolean}
Test for undefined.
Parameters:
Name | Type | Description |
---|---|---|
obj |
mixed | Object to test. |
Returns:
- Type
- Boolean
listen(obj, type, fn)
Listen to an event.
Parameters:
Name | Type | Description |
---|---|---|
obj |
object | DOM Node to listen for events on. |
type |
string | Event type, e.g. "click". |
fn |
function | Function to call when event fires. |
merge(mergeTo, mergeFrom, optionsopt) → {object}
Merge properties from one object to another.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
mergeTo |
object | Object to merge to. | ||
mergeFrom |
object | Object to merge from. | ||
options |
object |
<optional> |
Merge options. | |
options.clone |
bool |
<optional> |
false | Clone the object rather than augment. |
options.shallow |
bool |
<optional> |
false | Shallow merge. |
Returns:
- Type
- object
onFrame(fn, scopeopt, argsopt)
Listen for animation frame (requestAnimationFrame).
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
fn |
function | Function to execute for each frame. | ||
scope |
object |
<optional> |
window | Scope to execute function in. |
args |
mixed |
<optional> |
Any further arguments get passed to the function. Note that the last argument will be the timestamp of when the frame is executed. |
parent(el, levelopt) → {object}
Get the parent or ancestor of a given element.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
el |
object | DOM Node. | ||
level |
int | function |
<optional> |
1 | The number of levels to traverse or a function to test each node against. The function should return truthy if the desired node is located. Traversal will stop without a result and return `undefined` if the function returns -1. |
Returns:
DOM Node or null.
- Type
- object
parseQuery(str) → {object}
Parse a query string into an object of key/value pairs.
Parameters:
Name | Type | Description |
---|---|---|
str |
Optional query string, otherwise use current URL. Note that parameters without a value will be assigned a value of 1. |
Returns:
Query params object.
- Type
- object
processEvent(e, preventopt, stopopt) → {object}
Process an event.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
e |
object | Event object. | |
prevent |
boolean |
<optional> |
Prevent default. |
stop |
boolean |
<optional> |
Stop propagation. |
Returns:
Event target.
- Type
- object
remix(obj, keys) → {object|boolean}
Pick keys from an object to generate a new object.
Parameters:
Name | Type | Description |
---|---|---|
obj |
object | Object to get values from. |
keys |
object | Object containing the desired keys. |
Returns:
New object or false on error.
- Type
- object | boolean
remove(el)
Remove an element from the DOM.
Parameters:
Name | Type | Description |
---|---|---|
el |
object | DOM Node |
render(frag, data) → {DocumentFragment|HTMLElement}
Use a document fragment or node to render a new piece of DOM.
Parameters:
Name | Type | Description |
---|---|---|
frag |
DocumentFragment | HTMLElement | Document fragment to clone or element to render to. |
data |
object | Data to render. |
Returns:
Cloned and rendered fragment ready to append to your document or element which was rendered to.
- Type
- DocumentFragment | HTMLElement
replace(old, new)
Safely replace one element with another.
Parameters:
Name | Type | Description |
---|---|---|
old |
HTMLElement | Element to replace. |
new |
HTMLElement | Element to replace with. |
request(url, callbackopt, optsopt) → {object}
Make an XHR request.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
url |
string | URL. | ||
callback |
function |
<optional> |
Callback function which should expect `error`, `response` (text or JSON object), and `request` (XMLHttpRequest object) arguments. | |
opts |
object |
<optional> |
Options containing any of: | |
opts.data |
string |
<optional> |
Data to send in the request body. | |
opts.headers |
object |
<optional> |
Key/value collection of headers to include in the request. | |
opts.json |
object |
<optional> |
Object which will be JSON encoded and sent in the request body (see data). | |
opts.method |
string |
<optional> |
'GET' | HTTP method to use in the request. |
opts.parseJson |
object |
<optional> |
Parse the response as JSON. This will happen by default if the `Content-Type` response header contains "json". | |
opts.props |
object |
<optional> |
Arbitrary properties set directly to the `XMLHttpRequest` object. |
Returns:
XMLHttpRequest object.
- Type
- object
resolvePrefix(prop, objopt, loweropt) → {string}
Get the vendor prefixed property name.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
prop |
string | CSS property in camelCase to resolve. | |
obj |
object |
<optional> |
Optional DOM object to test property against. |
lower |
boolean |
<optional> |
Use camelCase instead of CapitalCase. |
Returns:
Prefixed property.
- Type
- string
resolveProperty(prop) → {string}
Get the vendor prefixed property name.
Parameters:
Name | Type | Description |
---|---|---|
prop |
string | CSS property in dash-case to prefix. |
Returns:
Vendor prefix.
- Type
- string
set(obj, path, val, conditionalopt) → {object}
Set a value [deep] in an object.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
obj |
object | Object to set a value on. | ||
path |
string | array | Property path, e.g. "foo.bar.baz" or ["foo", "bar", "baz"] | ||
val |
mixed | Value to set. | ||
conditional |
boolean |
<optional> |
false | Only set value if one is not already present. |
Returns:
Object.
- Type
- object
setAttrs(el, attrs, eventsopt) → {object}
Set attributes and properties on an element.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
el |
HTMLElement | DocumentFragment | Node | |
attrs |
object | string | A string or key/value pairs including special handling for: | |
attrs.className |
string |
<optional> |
Class name. |
attrs.innerHTML |
string |
<optional> |
Content to set using innerHTML (also how `attrs`is handled when a string ). |
attrs.parentNode |
HTMLElement | DocumentFragment |
<optional> |
Parent node to append to. |
attrs.children |
array | HTMLElement | DocumentFragment |
<optional> |
Array of nodes or a single node to append. |
attrs.styles |
object |
<optional> |
Style object which is passed to `setStyles`. |
attrs.<attr> |
string |
<optional> |
Anything else will go through `el.setAttribute`. |
events |
object |
<optional> |
Optional collection of listener functions keyed by event name, as passed to `listen`. |
Returns:
The DOM Node.
- Type
- object
setClass(el, class)
Set the class on a DOM Node.
Parameters:
Name | Type | Description |
---|---|---|
el |
object | DOM Node to set the class on. |
class |
string | The class to set. |
setDoc(doc) → {HTMLDocument}
Set the document used by SimpleUtil. Useful for Node.js.
Parameters:
Name | Type | Description |
---|---|---|
doc |
HTMLDocument | Document to set. |
Returns:
The document passed in.
- Type
- HTMLDocument
setStyle(obj, style, val, resolveopt)
Set a CSS property on a DOM Node.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
obj |
object | DOM Node to set a CSS property on. | ||
style |
string | CSS property in camelCase. | ||
val |
string | Property value. Note that "px" will automatically be appended to numeric values. | ||
resolve |
boolean |
<optional> |
false | Resolve vendor prefix. |
setStyles(obj, styles, resolveopt)
Set multiple CSS properties on a DOM Node.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
obj |
object | DOM Node to set CSS properties on. | ||
styles |
string | Object containing key/values for CSS properties in camelCase. | ||
resolve |
boolean |
<optional> |
false | Resolve vendor prefix. |
trim(str) → {string}
Trim whitespace from the beginning and ending of a string.
Parameters:
Name | Type | Description |
---|---|---|
str |
string | String to trim. |
Returns:
Trimmed string.
- Type
- string