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.jsorthin.min.jsfile into your website. - Add the utility to the pages that requires it with a
scripttag.
<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
forEachmethod is added to theNodeListinterface which loops over the nodes it contains. - Group Methods: Several methods relating to classes, attributes, and events that are available on the
Elementinterface are added to theNodeListinterface as well, so that methods can be called on allElements in aNodeListat 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:
ElementandNodeListmethods will return a reference to themselves when a specific value is not expected. - Shortcut Function: The
Thinfunction is included which is an alias fordocument.querySelectorAllas well as several other useful tasks (see below). In conjunction with the Group Methods mentioned above, a query for a singleElementwill 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 anyElementthat 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") orwindowload(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
truewill execute the listener upon thewindowloadevent being fired. A value offalsewill execute the listener upon thedocumentDOMContentLoadedevent being fired.
- Returns
undefined
- listener
####NodeList Casting####
Thin(object)Added
Takes the passed object and returns it as aNodeList.- object
ElementorNodeList- 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 theElementhas 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
StringorObject- 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
setPropertyAdded
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
StringorObject- 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 theElementproperty. If the property is non-existent thenundefinedwill 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 thehasOwnPropertymethod, 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 eachElementand 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 theElementitself.
- thisArg
Object- The object which will be bound to the
thisvariable when thecallbackis called.
- Returns
NodeList
- callback
addClass(name[, ...])Added
Adds one or more classes to eachElementin 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 eachElementin theNodeList.- name
String- A name to remove from the class attribute.
- ...
String- Any additional names to remove.
- Returns
NodeList
- name
hasClass(name)Added
Whether anyElementin theNodeListhas the class.- name
String- The name to check for.
- Returns
Boolean
- name
getAttribute(name)Added
Gets the value of the attribute on the firstElementin 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 eachElementin theNodeList. An object of key/value pairs may be used to set multiple attributes.- name
StringorObject- 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 eachElementin theNodeList.- name
String- An attribute to remove from the class.
- ...
String- Any additional attributes to remove.
- Returns
NodeList
- name
hasAttribute(name)Added
Whether anyElementin theNodeListhas the attribute.- name
String- The attribute to check.
- Returns
Boolean
- name
setPropertyAdded
Sets one or more properties on eachElementin theNodeList. An object of key/value pairs may be used to set multiple properties. Non-existent properties will not be set.- name
StringorObject- 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 firstElementin theNodeList. If the property is non-existent thenundefinedwill be returned.- name
String- The property to return the value of.
- Returns
Any
- name
hasProperty(name)Added
Whether the property exists on anyElementin 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 -
NodeListconforming accepts multiple parameters. - v1.2.0 -
NodeListconforming and subquery selection added. - v1.1.1 - Bug with load bindings fixed.
- v1.1.0 - Additional load binding shortcuts available on the
Thinfunction. - v1.0.0 - Initial release.
The Thin JavaScript utility is open-sourced software licensed under the MIT license