Thin is a small utility which extends the default behavior of the Element
and NodeList
JavaScript interfaces to make working with the DOM easier and require less code. This utility was developed specifically for modern browsers using standardized ES5 and ES6 features, with careful thought put into making the syntax intuitive and familiar to those who have worked with other JavaScript libraries.
Installation
Features
Method Reference
Browser Compatibility
Release Notes
License
- Download or clone this repository and copy either the
thin.js
orthin.min.js
file into your website. - Add the utility to the pages that requires it with a
script
tag.
<script src="path/to/thin.min.js type="text/javascript"></script>
Make sure that it is the first script loaded if possible. At the very least it should be loaded before any other scripts that uses its methods. - Start using the utitilty! Take a look at the Features and Method Reference sections for information on how to get started.
- NodeList Looping: A
forEach
method is added to theNodeList
interface which loops over the nodes it contains. - Group Methods: Several methods relating to classes, attributes, and events that are available on the
Element
interface are added to theNodeList
interface as well, so that methods can be called on allElement
s in aNodeList
at once. - Property Methods: These are a shortcut for setting properties directly or by using the array syntax (
Element[propertyName]
), they also have the added bonus of only affecting properties that exist on the interface already. References to non-existent properties will simply be ignored. - Method Chaining:
Element
andNodeList
methods will return a reference to themselves when a specific value is not expected. - Shortcut Function: The
Thin
function is included which is an alias fordocument.querySelectorAll
as well as several other useful tasks (see below). In conjunction with the Group Methods mentioned above, a query for a singleElement
will have the same methods available without needing to extract it from theNodeList
.
Next to each method name it is stated whether the method was been Added or the existing method was Altered
###Thin Shortcut Function###
The Thin
function can perform different shortcut tasks depending on the parameters passed to it.
####Query Selector####
Thin(query)
Added
Returns anyElement
that matches the query.- query
String
- The query to match against.
- Returns
NodeList
- query
####Page Load Binding####
Thin(listener[, waitForAll])
Added
Binds the listener to either theDOMContentLoaded
(document.readyState
=="interactive"
) orwindow
load
(document.readyState
=="complete"
) event.- listener
Function
- The code to be executed upon the load event being fired.
- waitForAll
Boolean
- Whether to wait for all resources to be loaded. A value of
true
will execute the listener upon thewindow
load
event being fired. A value offalse
will execute the listener upon thedocument
DOMContentLoaded
event being fired.
- Returns
undefined
- listener
####NodeList Casting####
Thin(object)
Added
Takes the passed object and returns it as aNodeList
.- object
Element
orNodeList
- The object to be converted to a
NodeList
.
- Returns
NodeList
- object
###Element Interface###
addClass(name[, ...])
Added
Adds one or more classes to theElement
.- name
String
- A name to add to the class attribute.
- ...
String
- Any additional names to add.
- Returns
Element
- name
removeClass(name[, ...])
Added
Removes one or more classes from theElement
.- name
String
- A name to remove from the class attribute.
- ...
String
- Any additional names to remove.
- Returns
Element
- name
hasClass(name)
Added
Whether theElement
has the class.- name
String
- The name to check for.
- Returns
Boolean
- name
setAttribute(name[, value])
Altered
Sets the value of one or more attributes on theElement
. An object of key/value pairs may be used to set multiple attributes.- name
String
orObject
- If a string is passed then the attribute with that name is assigned the passed
value
. If an object is passed then attributes are assigned by the key/value pairs.
- value
Any
- If a single attribute is being set, this should be the value to set it to.
- Returns
Element
- name
removeAttribute(name[, ...])
Altered
Removes one or more attributes from theElement
.- name
String
- An attribute to remove from the class.
- ...
String
- Any additional attributes to remove.
- Returns
Element
- name
setProperty
Added
Sets one or more properties on theElement
. An object of key/value pairs may be used to set multiple properties. Non-existent properties will not be set.- name
String
orObject
- If a string is passed then the property with that name is assigned the passed
value
. If an object is passed then attributes are assigned by the key/value pairs.
- value
Any
- If a single property is being set, this should be the value to set it to.
- Returns
Element
- name
getProperty(name)
Added
Gets the value of theElement
property. If the property is non-existent thenundefined
will be returned.- name
String
- The property to return the value of.
- Returns
Any
- name
hasProperty(name)
Added
Whether the property exists on theElement
. This method differs from thehasOwnProperty
method, which only compares to the properties of the current prototype and not inherited ones.- name
String
- The property to check for.
- Returns
Boolean
- name
addEventListener(type, listener[, useCapture])
Altered
This method functions identically to the native implementation, but returns itself for chaining.- Returns
Element
- Returns
removeEventLister(type, listener[, useCapture])
Altered
This method functions identically to the native implementation, but returns itself for chaining.- Returns
Element
- Returns
###NodeList Interface###
forEach(callback[, thisArg])
Added
Loops through eachElement
and applies the callback to each one.- callback
Function
- The function to be called on each
Element
. This function will be passed a single argument which is theElement
itself.
- thisArg
Object
- The object which will be bound to the
this
variable when thecallback
is called.
- Returns
NodeList
- callback
addClass(name[, ...])
Added
Adds one or more classes to eachElement
in theNodeList
.- name
String
- A name to add to the class attribute.
- ...
String
- Any additional names to add.
- Returns
NodeList
- name
removeClass(name[, ...])
Added
Removes one or more classes from eachElement
in theNodeList
.- name
String
- A name to remove from the class attribute.
- ...
String
- Any additional names to remove.
- Returns
NodeList
- name
hasClass(name)
Added
Whether anyElement
in theNodeList
has the class.- name
String
- The name to check for.
- Returns
Boolean
- name
getAttribute(name)
Added
Gets the value of the attribute on the firstElement
in theNodeList
.- name
String
- The attribute to return the value of.
- Returns
Any
- name
setAttribute(name[, value])
Added
Sets the value of one or more attributes on eachElement
in theNodeList
. An object of key/value pairs may be used to set multiple attributes.- name
String
orObject
- If a string is passed then the attribute with that name is assigned the passed
value
. If an object is passed then attributes are assigned by the key/value pairs.
- value
Any
- If a single attribute is being set, this should be the value to set it to.
- Returns
NodeList
- name
removeAttribute(name[, ...])
Added
Removes one or more attributes from eachElement
in theNodeList
.- name
String
- An attribute to remove from the class.
- ...
String
- Any additional attributes to remove.
- Returns
NodeList
- name
hasAttribute(name)
Added
Whether anyElement
in theNodeList
has the attribute.- name
String
- The attribute to check.
- Returns
Boolean
- name
setProperty
Added
Sets one or more properties on eachElement
in theNodeList
. An object of key/value pairs may be used to set multiple properties. Non-existent properties will not be set.- name
String
orObject
- If a string is passed then the property with that name is assigned the passed
value
. If an object is passed then attributes are assigned by the key/value pairs.
- value
Any
- If a single property is being set, this should be the value to set it to.
- Returns
NodeList
- name
getProperty(name)
Added
Gets the value of the property on the firstElement
in theNodeList
. If the property is non-existent thenundefined
will be returned.- name
String
- The property to return the value of.
- Returns
Any
- name
hasProperty(name)
Added
Whether the property exists on anyElement
in theNodeList
.- name
String
- The property to check for.
- Returns
Boolean
- name
addEventListener(type, listener, useCapture)
Added
This method uses the specifications found here:
http://www.w3.org/TR/dom/#eventtarget
https://developer.mozilla.org/en-US/docs/Web/API/EventTarget.addEventListener- Returns
NodeList
- Returns
removeEventListener(type, listener, useCapture)
Added
This method uses the specifications found here:
http://www.w3.org/TR/dom/#eventtarget
https://developer.mozilla.org/en-US/docs/Web/API/EventTarget.removeEventListener- Returns
NodeList
- Returns
querySelector(selector)
Added
Returns the first element to match the selector that is a descendant of one of the elements in theNodeList
.- selector
String
- The selector to match the elements against.
- Returns
Element
- selector
querySelectorAll(selector)
Added
Returns all elements that match the selector which are descendants of the elements in theNodeList
.- selector
String
- The selector to match the elements against.
- Returns
NodeList
- selector
- Chrome: 8.0+
- Firefox: 6.0+
- Internet Explorer: 10.0+
- Opera: 12.10+
- Safari: 5.1+
Additional information can be found in the CHANGELOG.md file
- v1.3.0 - Switch to Jasmine for testing.
- v1.2.1 -
NodeList
conforming accepts multiple parameters. - v1.2.0 -
NodeList
conforming and subquery selection added. - v1.1.1 - Bug with load bindings fixed.
- v1.1.0 - Additional load binding shortcuts available on the
Thin
function. - v1.0.0 - Initial release.
The Thin JavaScript utility is open-sourced software licensed under the MIT license