@fr0st/query v3.3.0
fQuery
fQuery is a free, open-source DOM manipulation library for JavaScript.
It is a lightweight (~16kb gzipped) and modern library, utilizing ES6 syntax and features including Promises.
Table Of Contents
- Installation
- Basic Usage
- Ajax
- Animation
- Attributes
- Cookie
- Events
- Manipulation
- Parsing
- Queue
- Scripts
- Stylesheets
- Traversal
- Utility
Installation
In Browser
<script type="text/javascript" src="/path/to/fquery.min.js"></script>Using NPM
npm i @fr0st/queryIn Node.js:
import { JSDOM } from 'jsdom';
import registerGlobals from '@fr0st/query';
const window = new JSDOM('');
const $ = registerGlobals(window);Basic Usage
selectoris a query selector string, a Node, HTMLElement, DocumentFragment, ShadowRoot, Document, Window, NodeList, HTMLCollection, QuerySet, an array of nodes or a function to execute when the DOM is ready.contextis a query selector string, a HTMLElement, DocumentFragment, ShadowRoot, Document, HTMLCollection, QuerySet or an array of nodes, and will default to the Document context of thedom.
const query = $(selector, context);This method returns a QuerySet.
Query One
You can also query for a single node using the queryOne method.
selectoris a query selector string, a Node, HTMLElement, DocumentFragment, ShadowRoot, Document, Window, NodeList, HTMLCollection, QuerySet, or an array of nodes.contextis a query selector string, a HTMLElement, DocumentFragment, ShadowRoot, Document, HTMLCollection, QuerySet or an array of nodes, and will default to the Document context of thedom.
const query = $.queryOne(selector, context);This method returns a QuerySet.
No Conflict
Reset the global $ variable.
$.noConflict();You can continue to use this library by using the global fQuery variable.
Core Methods
This library also includes all functions from the FrostCore library, accessible from the $ global variable (with an underscore prefix).
const random = $._random();Ajax
Ajax
Perform an XHR request.
optionsis an object containing options for the request.urlis a string containing the URL for the request, and will default to the current window location.methodis a string containing the method to use for the request, and will default to "GET".datacan be an object, array, string or FormData containing data to send with the request, and will default to null.contentTypeis a string containing the Content-Type header to send with the request, and will default to "application/x-www-form-urlencoded".responseTypeis a string containing the expected Content-Type header of the response.mimeTypeis a string containing the MIME type to use for the server response.usernameis a string containing the username to authenticate with.passwordis a string containing the password to authenticate with.timeoutis a number indicating the number of milliseconds before the request will be terminated, and will default to 0.isLocalis a boolean indicating whether the request will be treated as local.cacheis a boolean indicating whether to cache the request, and will default to true.processDatais a boolean indicating whether to process the data depending on thecontentType, and will default to true.rejectOnCancelis a boolean indicating whether to reject the promise if the request is cancelled, and will default to true.headersis an object containing additional headers to send with the request.afterSendis a function that accepts anxhrargument, and will be called after the request is sent.beforeSendis a function that accepts anxhrargument, and will be called before the request is sent.onProgressis a function that acceptsprogress,xhrandeventas arguments and will be called on XHR download progress.onUploadProgressis a function that acceptsprogress,xhrandeventas arguments and will be called on XHR upload progress.
This method returns an AjaxRequest that resolves when the request is completed, or rejects on failure.
const request = $.ajax(options);Delete
Perform an XHR DELETE request.
urlis a string containing the URL for the request.optionsis an object containing options for the request.methodis a string containing the method to use for the request, and will default to "DELETE".contentTypeis a string containing the Content-Type header to send with the request, and will default to "application/x-www-form-urlencoded".responseTypeis a string containing the expected Content-Type header of the response.mimeTypeis a string containing the MIME type to use for the server response.usernameis a string containing the username to authenticate with.passwordis a string containing the password to authenticate with.timeoutis a number indicating the number of milliseconds before the request will be terminated, and will default to 0.isLocalis a boolean indicating whether the request will be treated as local.cacheis a boolean indicating whether to cache the request, and will default to true.processDatais a boolean indicating whether to process the data depending on thecontentType, and will default to true.rejectOnCancelis a boolean indicating whether to reject the promise if the request is cancelled, and will default to true.headersis an object containing additional headers to send with the request.afterSendis a function that accepts anxhrargument, and will be called after the request is sent.beforeSendis a function that accepts anxhrargument, and will be called before the request is sent.onProgressis a function that acceptsprogress,xhrandeventas arguments and will be called on XHR download progress.onUploadProgressis a function that acceptsprogress,xhrandeventas arguments and will be called on XHR upload progress.
This method returns an AjaxRequest that resolves when the request is completed, or rejects on failure.
const request = $.delete(url, options);Get
Perform an XHR GET request.
urlis a string containing the URL for the request.datacan be an object, array, string containing data to send with the request, and will default to null.optionsis an object containing options for the request.methodis a string containing the method to use for the request, and will default to "GET".contentTypeis a string containing the Content-Type header to send with the request, and will default to "application/x-www-form-urlencoded".responseTypeis a string containing the expected Content-Type header of the response.mimeTypeis a string containing the MIME type to use for the server response.usernameis a string containing the username to authenticate with.passwordis a string containing the password to authenticate with.timeoutis a number indicating the number of milliseconds before the request will be terminated, and will default to 0.isLocalis a boolean indicating whether the request will be treated as local.cacheis a boolean indicating whether to cache the request, and will default to true.processDatais a boolean indicating whether to process the data depending on thecontentType, and will default to true.rejectOnCancelis a boolean indicating whether to reject the promise if the request is cancelled, and will default to true.headersis an object containing additional headers to send with the request.afterSendis a function that accepts anxhrargument, and will be called after the request is sent.beforeSendis a function that accepts anxhrargument, and will be called before the request is sent.onProgressis a function that acceptsprogress,xhrandeventas arguments and will be called on XHR download progress.onUploadProgressis a function that acceptsprogress,xhrandeventas arguments and will be called on XHR upload progress.
This method returns an AjaxRequest that resolves when the request is completed, or rejects on failure.
const request = $.get(url, data, options);Patch
Perform an XHR PATCH request.
urlis a string containing the URL for the request.datacan be an object, array, string or FormData containing data to send with the request, and will default to null.optionsis an object containing options for the request.methodis a string containing the method to use for the request, and will default to "PATCH".contentTypeis a string containing the Content-Type header to send with the request, and will default to "application/x-www-form-urlencoded".responseTypeis a string containing the expected Content-Type header of the response.mimeTypeis a string containing the MIME type to use for the server response.usernameis a string containing the username to authenticate with.passwordis a string containing the password to authenticate with.timeoutis a number indicating the number of milliseconds before the request will be terminated, and will default to 0.isLocalis a boolean indicating whether the request will be treated as local.cacheis a boolean indicating whether to cache the request, and will default to true.processDatais a boolean indicating whether to process the data depending on thecontentType, and will default to true.rejectOnCancelis a boolean indicating whether to reject the promise if the request is cancelled, and will default to true.headersis an object containing additional headers to send with the request.afterSendis a function that accepts anxhrargument, and will be called after the request is sent.beforeSendis a function that accepts anxhrargument, and will be called before the request is sent.onProgressis a function that acceptsprogress,xhrandeventas arguments and will be called on XHR download progress.onUploadProgressis a function that acceptsprogress,xhrandeventas arguments and will be called on XHR upload progress.
This method returns an AjaxRequest that resolves when the request is completed, or rejects on failure.
const request = $.patch(url, data, options);Post
Perform an XHR POST request.
urlis a string containing the URL for the request.datacan be an object, array, string or FormData containing data to send with the request, and will default to null.optionsis an object containing options for the request.methodis a string containing the method to use for the request, and will default to "POST".contentTypeis a string containing the Content-Type header to send with the request, and will default to "application/x-www-form-urlencoded".responseTypeis a string containing the expected Content-Type header of the response.mimeTypeis a string containing the MIME type to use for the server response.usernameis a string containing the username to authenticate with.passwordis a string containing the password to authenticate with.timeoutis a number indicating the number of milliseconds before the request will be terminated, and will default to 0.isLocalis a boolean indicating whether the request will be treated as local.cacheis a boolean indicating whether to cache the request, and will default to true.processDatais a boolean indicating whether to process the data depending on thecontentType, and will default to true.rejectOnCancelis a boolean indicating whether to reject the promise if the request is cancelled, and will default to true.headersis an object containing additional headers to send with the request.afterSendis a function that accepts anxhrargument, and will be called after the request is sent.beforeSendis a function that accepts anxhrargument, and will be called before the request is sent.onProgressis a function that acceptsprogress,xhrandeventas arguments and will be called on XHR download progress.onUploadProgressis a function that acceptsprogress,xhrandeventas arguments and will be called on XHR upload progress.
This method returns an AjaxRequest that resolves when the request is completed, or rejects on failure.
const request = $.post(url, data, options);Put
Perform an XHR PUT request.
urlis a string containing the URL for the request.datacan be an object, array, string or FormData containing data to send with the request, and will default to null.optionsis an object containing options for the request.methodis a string containing the method to use for the request, and will default to "PUT".contentTypeis a string containing the Content-Type header to send with the request, and will default to "application/x-www-form-urlencoded".responseTypeis a string containing the expected Content-Type header of the response.mimeTypeis a string containing the MIME type to use for the server response.usernameis a string containing the username to authenticate with.passwordis a string containing the password to authenticate with.timeoutis a number indicating the number of milliseconds before the request will be terminated, and will default to 0.isLocalis a boolean indicating whether the request will be treated as local.cacheis a boolean indicating whether to cache the request, and will default to true.processDatais a boolean indicating whether to process the data depending on thecontentType, and will default to true.rejectOnCancelis a boolean indicating whether to reject the promise if the request is cancelled, and will default to true.headersis an object containing additional headers to send with the request.afterSendis a function that accepts anxhrargument, and will be called after the request is sent.beforeSendis a function that accepts anxhrargument, and will be called before the request is sent.onProgressis a function that acceptsprogress,xhrandeventas arguments and will be called on XHR download progress.onUploadProgressis a function that acceptsprogress,xhrandeventas arguments and will be called on XHR upload progress.
This method returns an AjaxRequest that resolves when the request is completed, or rejects on failure.
const request = $.put(url, data, options);Animation
Animate
Add an animation to each node.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.callbackis a function that acceptsnode,progressandoptionsas arguments, wherenodeis a HTMLElement,progressis a value between 0 and 1 andoptionsis theoptionsobject passed to this method.optionsis an object containing options for how the animation should be handled.durationis the number of milliseconds that the animation should last, and will default to 1000.typeis a string of either ease-in, ease-out, ease-in-out or linear indicating the type of animation to run, and will default to ease-in-out.infiniteis a boolean indicating whether the animation should continue forever, and will default to false.
This method returns an AnimationSet that will resolve after the animation has completed.
const animation = $.animate(selector, callback, options);Stop Animations
Stop all animations for each node.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.optionsis an object containing options for how the animations should be stopped.finishis a boolean indicating whether to immediately finish the animation, and will default to true.
$.stop(selector, options);Animations
Drop In
Drop each node into place.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of node.optionsis an object containing options for how the animation should be handled.directionis a string or function that returns either "top", "right", "bottom" or "left" indicating the direction to drop from, and will default to "top".durationis the number of milliseconds that the animation should last, and will default to 1000.typeis a string of either "ease-in", "ease-out", "ease-in-out" or "linear" indicating the type of animation to run, and will default to "ease-in-out".infiniteis a boolean indicating whether the animation should continue forever, and will default to false.useGpuis a boolean indicating whether the animation should use GPU acceleration (CSS transform) and will default to true.
This method returns an AnimationSet that will resolve after the animation has completed.
const animation = $.dropIn(selector, options);Drop Out
Drop each node out of place.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.optionsis an object containing options for how the animation should be handled.directionis a string or function that returns either "top", "right", "bottom" or "left" indicating the direction to drop from, and will default to "top".durationis the number of milliseconds that the animation should last, and will default to 1000.typeis a string of either "ease-in", "ease-out", "ease-in-out" or "linear" indicating the type of animation to run, and will default to "ease-in-out".infiniteis a boolean indicating whether the animation should continue forever, and will default to false.useGpuis a boolean indicating whether the animation should use GPU acceleration (CSS transform) and will default to true.
This method returns an AnimationSet that will resolve after the animation has completed.
const animation = $.dropOut(selector, options);Fade In
Fade the opacity of each node in.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.optionsis an object containing options for how the animation should be handled.durationis the number of milliseconds that the animation should last, and will default to 1000.typeis a string of either "ease-in", "ease-out", "ease-in-out" or "linear" indicating the type of animation to run, and will default to "ease-in-out".infiniteis a boolean indicating whether the animation should continue forever, and will default to false.
This method returns an AnimationSet that will resolve after the animation has completed.
const animation = $.fadeIn(selector, options);Fade Out
Fade the opacity of each node out.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.optionsis an object containing options for how the animation should be handled.durationis the number of milliseconds that the animation should last, and will default to 1000.typeis a string of either "ease-in", "ease-out", "ease-in-out" or "linear" indicating the type of animation to run, and will default to "ease-in-out".infiniteis a boolean indicating whether the animation should continue forever, and will default to false.
This method returns an AnimationSet that will resolve after the animation has completed.
const animation = $.fadeOut(selector, options);Rotate In
Rotate each node in on an X, Y or Z.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.optionsis an object containing options for how the animation should be handled.xis the amount of rotation to apply to the X axis, and will default to 0.yis the amount of rotation to apply to the Y axis, and will default to 1.zis the amount of rotation to apply to the Z axis, and will default to 0.inverseis a boolean indicating whether to rotate in the opposite direction, and will default to false.durationis the number of milliseconds that the animation should last, and will default to 1000.typeis a string of either "ease-in", "ease-out", "ease-in-out" or "linear" indicating the type of animation to run, and will default to "ease-in-out".infiniteis a boolean indicating whether the animation should continue forever, and will default to false.
This method returns an AnimationSet that will resolve after the animation has completed.
const animation = $.rotateIn(selector, options);Rotate Out
Rotate each node out on an X, Y or Z.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.optionsis an object containing options for how the animation should be handled.xis the amount of rotation to apply to the X axis, and will default to 0.yis the amount of rotation to apply to the Y axis, and will default to 1.zis the amount of rotation to apply to the Z axis, and will default to 0.inverseis a boolean indicating whether to rotate in the opposite direction, and will default to false.durationis the number of milliseconds that the animation should last, and will default to 1000.typeis a string of either "ease-in", "ease-out", "ease-in-out" or "linear" indicating the type of animation to run, and will default to "ease-in-out".infiniteis a boolean indicating whether the animation should continue forever, and will default to false.
This method returns an AnimationSet that will resolve after the animation has completed.
const animation = $.rotateOut(selector, options);Slide In
Slide each node into place to a direction.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.optionsis an object containing options for how the animation should be handled.directionis a string or function that returns either "top", "right", "bottom" or "left" indicating the direction to slide from, and will default to "top".durationis the number of milliseconds that the animation should last, and will default to 1000.typeis a string of either "ease-in", "ease-out", "ease-in-out" or "linear" indicating the type of animation to run, and will default to "ease-in-out".infiniteis a boolean indicating whether the animation should continue forever, and will default to false.useGpuis a boolean indicating whether the animation should use GPU acceleration (CSS transform) and will default to true.
This method returns an AnimationSet that will resolve after the animation has completed.
const animation = $.slideIn(selector, options);Slide Out
Slide each node out of place from a direction.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.optionsis an object containing options for how the animation should be handled.directionis a string or function that returns either "top", "right", "bottom" or "left" indicating the direction to slide from, and will default to "top".durationis the number of milliseconds that the animation should last, and will default to 1000.typeis a string of either "ease-in", "ease-out", "ease-in-out" or "linear" indicating the type of animation to run, and will default to "ease-in-out".infiniteis a boolean indicating whether the animation should continue forever, and will default to false.useGpuis a boolean indicating whether the animation should use GPU acceleration (CSS transform) and will default to true.
This method returns an AnimationSet that will resolve after the animation has completed.
const animation = $.slideOut(selector, options);Squeeze In
Squeeze each node into place to a direction.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.optionsis an object containing options for how the animation should be handled.directionis a string or function that returns either "top", "right", "bottom" or "left" indicating the direction to squeeze from, and will default to "top".durationis the number of milliseconds that the animation should last, and will default to 1000.typeis a string of either "ease-in", "ease-out", "ease-in-out" or "linear" indicating the type of animation to run, and will default to "ease-in-out".infiniteis a boolean indicating whether the animation should continue forever, and will default to false.useGpuis a boolean indicating whether the animation should use GPU acceleration (CSS transform) and will default to true.
This method returns an AnimationSet that will resolve after the animation has completed.
const animation = $.squeezeIn(selector, options);Squeeze Out
Squeeze each node out of place from a direction.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.optionsis an object containing options for how the animation should be handled.directionis a string or function that returns either "top", "right", "bottom" or "left" indicating the direction to squeeze from, and will default to "top".durationis the number of milliseconds that the animation should last, and will default to 1000.typeis a string of either "ease-in", "ease-out", "ease-in-out" or "linear" indicating the type of animation to run, and will default to "ease-in-out".infiniteis a boolean indicating whether the animation should continue forever, and will default to false.useGpuis a boolean indicating whether the animation should use GPU acceleration (CSS transform) and will default to true.
This method returns an AnimationSet that will resolve after the animation has completed.
const animation = $.squeezeOut(selector, options);Attributes
Get Attribute
Get an attribute value for the first node.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.attributeis a string indicating the attribute value to return.
const value = $.getAttribute(selector, attribute);If the attribute argument is omitted, an object containing all attribute values will be returned instead.
const attributes = $.getAttribute(selector);Get Dataset
Get a dataset value for the first node.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.keyis a string indicating the dataset value to return.
const value = $.getDataset(selector, key);If the key argument is omitted, an object containing all dataset values will be returned instead.
const dataset = $.getDataset(selector);This method will attempt to convert string values to JS primitives (including booleans, numbers, objects, arrays and null).
Get HTML
Get the HTML contents of the first node.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.
const html = $.getHTML(selector);Get Property
Get a property value for the first node.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.propertyis a string indicating the property value to return.
const value = $.getProperty(selector, property);Get Text
Get the text contents of the first node.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.
const text = $.getText(selector);Get Value
Get the value property of the first node.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.
const value = $.getValue(selector);Remove Attribute
Remove an attribute from each node.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.attributeis a string indicating the attribute value to remove.
$.removeAttribute(selector, attribute);Remove Dataset
Remove a dataset value from each node.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.keyis a string indicating the dataset value to remove.
$.removeDataset(selector, key);Remove Property
Remove a property from each node.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.propertyis a string indicating the property value to remove.
$.removeProperty(selector, property);Set Attribute
Set attributes for each node.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.attributeis a string indicating the attribute value to set.valueis the value you want to set the attribute to.
$.setAttribute(selector, attribute, value);Alternatively, you can set multiple attributes by passing a single attributes object as the argument with key/value pairs of the attributes to set.
$.setAttribute(selector, attributes);Set Dataset
Set dataset values for each node.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.keyis a string indicating the dataset value to set.valueis the value you want to set the dataset to.
$.setDataset(selector, key, value);Alternatively, you can set multiple dataset properties by passing a single dataset object as the argument with key/value pairs of the properties to set.
$.setDataset(selector, dataset);This method will convert object and array values to JSON strings.
Set HTML
Set the HTML contents for each node.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of node.htmlis a string that will become the HTML contents of the node.
$.setHTML(selector, html);Set Property
Set property values for each node.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.propertyis a string indicating the property value to set.valueis the value you want to set the property to.
$.setProperty(selector, property, value);Alternatively, you can set multiple properties by passing a single properties object as the argument with key/value pairs of the properties to set.
$.setProperty(selector, properties);Set Text
Set the text contents for each node.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.textis a string that will become the text contents of the node.
$.setText(selector, text);Set Value
Set the value property for each node.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.valueis a string that will become the value of the node.
$.setValue(selector, value);Custom Data
Clone Data
Clone custom data from each node to each other node.
selectoris a query selector string, a HTMLElement, DocumentFragment, ShadowRoot, Document, Window, NodeList, HTMLCollection, QuerySet or an array of nodes.otherSelectoris a query selector string, a HTMLElement, DocumentFragment, ShadowRoot, Document, Window, NodeList, HTMLCollection, QuerySet or an array of nodes.
$.cloneData(selector, otherSelector);Get Data
Get custom data for the first node.
selectoris a query selector string, a HTMLElement, DocumentFragment, ShadowRoot, Document, Window, NodeList, HTMLCollection, QuerySet or an array of nodes.keyis a string indicating the custom data value to return.
const value = $.getData(selector, key);If the key argument is omitted, an object containing all custom data values will be returned instead.
const data = $.getData(selector);Remove Data
Remove custom data for each node.
selectoris a query selector string, a HTMLElement, DocumentFragment, ShadowRoot, Document, Window, NodeList, HTMLCollection, QuerySet or an array of nodes.keyis a string indicating the custom data value to remove.
$.removeData(selector, key);Set Data
Set custom data for each node.
selectoris a query selector string, a HTMLElement, DocumentFragment, ShadowRoot, Document, Window, NodeList, HTMLCollection, QuerySet or an array of nodes.keyis a string indicating the custom data value to set.valueis the value you want to set the attribute to.
$.setData(selector, key, value);Alternatively, you can set multiple data values by passing a single data object as the argument with key/value pairs of the data to set.
$.setData(selector, data);Position
Center
Get the X,Y co-ordinates for the center of the first node.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.optionsis an object containing options for how the position should be calculated.offsetis a boolean indicating whether the co-ordinates should be offset from the top left of the document, and will default to false.
const center = $.center(selector, options);Constrain
Constrain each node to a container node.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.containerSelectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.
$.constrain(selector, containerSelector);Distance To
Get the distance of the first node to an X,Y position.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.xis a distance (in pixels) along the X axis.yis a distance (in pixels) along the Y axis.optionsis an object containing options for how the distance should be calculated.offsetis a boolean indicating whether the co-ordinates should be offset from the top left of the document, and will default to false.
const dist = $.distTo(selector, x, y, options);Distance To Node
Get the distance between two nodes.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.otherSelectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.
const dist = $.distToNode(selector, otherSelector);Nearest To
Get the nearest node to an X,Y position.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.xis a distance (in pixels) along the X axis.yis a distance (in pixels) along the Y axis.optionsis an object containing options for how the distance should be calculated.offsetis a boolean indicating whether the co-ordinates should be offset from the top left of the document, and will default to false.
const nearest = $.nearestTo(selector, x, y, options);Nearest To Node
Get the nearest node to another node.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.otherSelectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.
const nearest = $.nearestToNode(selector, otherSelector);Percent X
Get the percentage of an X co-ordinate relative to the first node.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.xis a distance (in pixels) along the X axis.optionsis an object containing options for how the percent should be calculated.offsetis a boolean indicating whether the co-ordinates should be offset from the top left of the document, and will default to false.clampis a boolean indicating whether to clamp the percent betwen 0 and 100, and will default to true.
const percentX = $.percentX(selector, x, options);Percent Y
Get the percentage of a Y co-ordinate relative to the first node.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.yis a distance (in pixels) along the Y axis.optionsis an object containing options for how the percent should be calculated.offsetis a boolean indicating whether the co-ordinates should be offset from the top left of the document, and will default to false.clampis a boolean indicating whether to clamp the percent betwen 0 and 100, and will default to true.
const percentY = $.percentY(selector, y, options);Position
Get the X,Y position for the top/left of the first node.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.optionsis an object containing options for how the position should be calculated.offsetis a boolean indicating whether the co-ordinates should be offset from the top left of the document, and will default to false.
const position = $.position(selector, options);Rectangle
Get the computed bounding rectangle of the first node.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.optionsis an object containing options for how the bounding rectangle should be calculated.offsetis a boolean indicating whether the rectangle should be offset from the top left of the document, and will default to false.
const rect = $.rect(selector, options);Scroll
Get Scroll X
Get the scroll X position of the first node.
selectoris a query selector string, a HTMLElement, Document, Window, NodeList, HTMLCollection, QuerySet or an array of nodes.
const scrollX = $.getScrollX(selector);Get Scroll Y
Get the scroll Y position of the first node.
selectoris a query selector string, a HTMLElement, Document, Window, NodeList, HTMLCollection, QuerySet or an array of nodes.
const scrollY = $.getScrollY(selector);Set Scroll
Scroll each node to an X,Y position.
selectoris a query selector string, a HTMLElement, Document, Window, NodeList, HTMLCollection, QuerySet or an array of nodes.xis a distance (in pixels) along the X axis to scroll to.yis a distance (in pixels) along the Y axis to scroll to.
$.setScroll(selector, x, y);Set Scroll X
Scroll each node to an X position.
selectoris a query selector string, a HTMLElement, Document, Window, NodeList, HTMLCollection, QuerySet or an array of nodes.xis a distance (in pixels) along the X axis to scroll to.
$.setScrollX(selector, x);Set Scroll Y
Scroll each node to a Y position.
selectoris a query selector string, a HTMLElement, Document, Window, NodeList, HTMLCollection, QuerySet or an array of nodes.yis a distance (in pixels) along the Y axis to scroll to.
$.setScrollY(selector, y);Size
Height
Get the computed height of the first node.
selectoris a query selector string, a HTMLElement, Document, Window, NodeList, HTMLCollection, QuerySet or an array of nodes.optionsis an object containing options for how the height should be calculated.boxSizeis a number indicating the box sizing to calculate. Allowed values are 0 (no padding), 1 (padding), 2 (padding and border), 3 (padding, border and margin) and 4 (scroll area), and will default to 1.outeris a boolean indicating whether to use the window outer height, and will default to false.
const height = $.height(selector, options);The following constants can also be used as the boxSize for brevity.
$.CONTENT_BOX$.PADDING_BOX$.BORDER_BOX$.MARGIN_BOX$.SCROLL_BOX
Width
Get the computed width of the first node.
selectoris a query selector string, a HTMLElement, Document, Window, NodeList, HTMLCollection, QuerySet or an array of nodes.optionsis an object containing options for how the width should be calculated.boxSizeis a number indicating the box sizing to calculate. Allowed values are 0 (no padding), 1 (padding), 2 (padding and border), 3 (padding, border and margin) and 4 (scroll area), and will default to 1.outeris a boolean indicating whether to use the window outer width, and will default to false.
const width = $.width(selector, options);The following constants can also be used as the boxSize for brevity.
$.CONTENT_BOX$.PADDING_BOX$.BORDER_BOX$.MARGIN_BOX$.SCROLL_BOX
Styles
Add Class
Add a class or classes to each node.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.classesis an array of classes, or a space seperated string of class names.
$.addClass(selector, ...classes);Computed Style
Get a computed CSS style value for the first node.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.styleis a string indicating the computed style property value to return.
const value = $.css(selector, style);If the style argument is omitted, an object containing all computed style values will be returned instead.
const css = $.css(selector);Get Style
Get a style property for the first node.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.styleis a string indicating the style property value to return.
const value = $.getStyle(selector, style);If the style argument is omitted, an object containing all style values will be returned instead.
const styles = $.getStyle(selector);Hide
Hide each node from display.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.
$.hide(selector);Remove Class
Remove a class or classes from each node.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.classesis an array of classes, or a space seperated string of class names.
$.removeClass(selector, ...classes);Remove Style
Remove a style property from each node.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.styleis a string indicating the style property to remove.
$.removeStyle(selector, style);Set Style
Set style properties for each node.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.styleis a string indicating the style property value to set.valueis the value you wish to set the style property to.optionsis an object containing options for how the style should be applied.importantis a boolean indicating the style should be set as important, and will default to false.
$.setStyle(selector, style, value, options);Alternatively, you can set multiple style properties by passing a single styles object as the argument with key/value pairs of the styles to set.
$.setStyle(selector, styles);Show
Display each hidden node.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.
$.show(selector);Toggle
Toggle the visibility of each node.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.
$.toggle(selector);Toggle Class
Toggle a class or classes for each node.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.classesis an array of classes, or a space seperated string of class names.
$.toggleClass(selector, ...classes);Cookie
Get Cookie
Get a cookie value.
nameis a string containing the name of the cookie value to retrieve.
const value = $.getCookie(name);Remove Cookie
Remove a cookie.
nameis a string containing the name of the cookie value to remove.optionsis an object containing configuration options for the cookie.expiresis a number indicating the number of seconds until the cookie will expire, and will default to -1.pathis a string indicating the path to use for the cookie.secureis a boolean indicating whether only set the cookie for secure requests, and will default to false.
$.removeCookie(name, options);Set Cookie
Set a cookie value.
nameis a string containing the name of the cookie value to set.valueis the value you wish to set the cookie to.optionsis an object containing configuration options for the cookie.expiresis a number indicating the number of seconds until the cookie will expire.pathis a string indicating the path to use for the cookie.secureis a boolean indicating whether only set the cookie for secure requests, and will default to false.
$.setCookie(name, value, options);Events
Blur
Trigger a blur event on the first node.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.
$.blur(selector);Click
Trigger a click event on the first node.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.
$.click(selector);Focus
Trigger a focus event on the first node.
selectoris a query selector string, a HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.
$.focus(selector);Ready
Add a function to the ready queue.
callbackis a function that will execute once the DOM has finished loading.
If the DOM is already loaded, callback will execute immediately.
$.ready(callback);Event Handlers
Add Event
Add events to each node.
selectoris a query selector string, a HTMLElement, ShadowRoot, Document, Window, NodeList, HTMLCollection, QuerySet or an array of nodes.eventsis a space-separated string of events to attach to the nodes.callbackis a function that accepts aneventargument, which will be called when the event is triggered.optionsis an object containing options for how the event should be added.captureis a boolean indicating whether to use a capture event, and will default to false.passiveis a boolean indicating whether to use a passive event, and will default to false.
$.addEvent(selector, events, callback, options);Add Event Delegate
Add delegated events to each node.
selectoris a query selector string, a HTMLElement, ShadowRoot, Document, NodeList, HTMLCollection, QuerySet or an array of nodes.eventsis a space-separated string of events to attach to the nodes.delegateis a query selector string which will only trigger the event if it is propagated by a target matching the selector.callbackis a function that accepts aneventargument, which will be called when the event is triggered.optionsis an object containing options for how the event should be added.captureis a boolean indicating whether to use a capture event, and will default to false.passiveis a boolean indicating whether to use a passive event, and will default to false.
$.addEventDelegate(selector, events, delegate, callback, options);Add Event Delegate Once
Add self-destructing delegated events to each node.
selectoris a query selector string, a HTMLElement, ShadowRoot, Document, NodeList, HTMLCollection, QuerySet or an array of nodes.eventsis a space-separated string of events to attach to the nodes.delegateis a query selector string which will only trigger the event if it is propagated by a target matching the selector.callbackis a function that accepts aneventargument, which will be called when the event is triggered.optionsis an object containing options for how the event should be added.captureis a boolean indicating whether to use a capture event, and will default to false.passiveis a boolean indicating whether to use a passive event, and will default to false.
$.addEventDelegateOnce(selector, events, delegate, callback, options);Add Event Once
Add self-destructing events to each node.
selectoris a query selector string, a HTMLElement, ShadowRoot, Document, Window, NodeList, HTMLCollection, QuerySet or an array of nodes.eventsis a space-separated string of events to attach to the nodes.callbackis a function that accepts aneventargument, which will be called when the event is triggered.optionsis an object containing options for how the event should be added.captureis a boolean indicating whether to use a capture event, and will default to false.passiveis a boolean indicating whether to use a passive event, and will default to false.
$.addEventOnce(selector, events, callback, options);Clone Events
Clone all events from each node to other nodes.
selectoris a query selector string, a HTMLElement, ShadowRoot, Document, Window, NodeList, HTMLCollection, QuerySet or an array of nodes.otherSelectoris a query selector string, a HTMLElement, ShadowRoot, Document, Window, NodeList, HTMLCollection, QuerySet or an array of nodes.
$.cloneEvents(selector, otherSelector);Remove Event
Remove events from each node.
selectoris a query selector string, a HTMLElement, ShadowRoot, Document, Window, NodeList, HTMLCollection, QuerySet or an array of nodes.eventsis a space-separated string of events to remove from the nodes.callbackis a function that accepts aneventargument, which will be called when the event is triggered.optionsis an object containing options for how the event should be removed.captureis a boolean indicating whether to use a capture event, and will default to null.
$.removeEvent(selector, events, callback, options);If the events, callback or capture arguments are omitted, this method will remove all matching events from each node.
Remove Event Delegate
Remove delegated events from each node.
selectoris a query selector string, a HTMLElement, ShadowRoot, Document, NodeList, HTMLCollection, QuerySet or an array of nodes.eventsis a space-separated string of events to remove from the nodes.delegateis a query selector string which will only trigger the event if it is propagated by a target matching the selector.callbackis a function that accepts aneventargument, which will be called when the event is triggered.optionsis an object containing options for how the event should be removed.captureis a boolean indicating whether to use a capture event, and will default to null.
$.removeEventDelegate(selector, events, delegate, callback, options);Trigger Event
Trigger events on each node.
selectoris a query selector string, a HTMLElement, ShadowRoot, Document, Window, NodeList, HTMLCollection, QuerySet or an array of nodes.eventsis a space-separated string of events to trigger on the nodes.optionsis an object containing options for creating the new event.datacan be used to attach additional data to the event.detailcan be used to attach additional details to the event.bubblesis a boolean indicating whether the event should bubble, and will default to true.cancelableis a boolean indicating whether the event is cancelable, and will default to true.
$.triggerEvent(selector, events, options);Trigger One
Trigger an event on the first node.
selectoris a query selector string, a HTMLElement, ShadowRoot, Document, Window, NodeList, HTMLCollection, QuerySet or an array of nodes.eventis an event to trigger on the nodes.optionsis an object containing options for creating the new event.datacan be used to attach additional data to the event.detailcan be used to attach additional details to the event.bubblesis a boolean indicating whether the event should bubble, and will default to true.cancelableis a boolean indicating whether the event is cancelable, and will default to true.
const cancelled = !$.triggerOne(selector, event, options);This method returns false if the event was cancelled, otherwise returns true.
Event Factory
Mouse Drag Event Factory
Create a mouse drag event (optionally limited by animation frame).
downis a function that accepts an event argument, which will be called when the event is started.moveis a function that accepts an event argument, which will be called when the mouse is moved during the event.upis a function that accepts an event argument, which will be called when the event has ended (mouse button has been released).optionsis an object containing configuration options for the drag event.debounceis a boolean indicating whether to debounce the move event, and will default to true.passiveis a boolean indicating whether to use passive event listeners, and will default to true.preventDefaultis a boolean indicating whether to prevent the default events, and will default to true.touchesis a number indicating the number of touches to trigger the event for, and will default to 1.
const drag = $.mouseDragFactory(down, move, up, options);This method also works with touch events.
Manipulation
Clone
Clone each node (optionally deep, and with events and data).
selectoris a query selector string, a Node, HTMLElement, DocumentFragment, NodeList, HTMLCollection, QuerySet or an array of nodes.optionsis an object containing options for how to clone the nodes.deepis a boolean indicating whether to also clone child nodes, and will default to true.eventsis a boolean indicating whether to also clone events, and will default to false.datais a boolean indicating whether to also clone data, and will default to false.animationsis a boolean indicating whether to also clone animations, and will default to false.
const clones = $.clone(selector, options);Detach
Detach each node from the DOM.
selectoris a query selector string, a Node, HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.
const detached = $.detach(selector);Empty
Remove all children of each node from the DOM.
selectoris a query selector string, a HTMLElement, DocumentFragment, ShadowRoot, Document, NodeList, HTMLCollection, QuerySet or an array of nodes.
$.empty(selector);Remove
Remove each node from the DOM.
selectoris a query selector string, a Node, HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.
$.remove(selector);Replace All
Replace each other node with nodes.
selectoris a query selector string, a HTML string, a Node, HTMLElement, DocumentFragment, NodeList, HTMLCollection, QuerySet or an array of nodes.otherSelectoris a query selector string, a Node, HTMLElement NodeList, HTMLCollection, QuerySet or an array of nodes.
$.replaceAll(selector, otherSelector);If a node you are replacing with is a DocumentFragment, the fragment contents will be used as a replacement.
If multiple nodes are being replaced, cloned nodes will be created for each except for the last one.
All events, data and animations will be removed from each node that is replaced.
Replace With
Replace each node with other nodes.
selectoris a query selector string, a Node, HTMLElement, NodeList, HTMLCollection, QuerySet or an array of nodes.otherSelectoris a query selector string, a HTML string, a Node, HTMLElement, DocumentFragment, NodeList, HTMLCollection, QuerySet or an array of nodes.
$.replaceWith(selector, otherSelector);If a node you are replacing with is a DocumentFragment, the fragment contents will be used as a replacement.
If multiple nodes are being replaced, cloned nodes will be created for each except for the last one.
All events, data and animations will be removed from each node that is replaced.
Create
Attach Shadow
Attach a shadow DOM tree to the first node.
selectoris a query selector string, aHTMLElement, HTMLCollection, QuerySet or an array of nodes.optionsis an object containing options for how the shadow should be attached.openis a boolean indicating whether the nodes are accessible from JavaScript outside the root, and will default to true.
const shadow = $.attachShadow(selector, options);Create
Create a new DOM element.
tagNameis a string indicating the type of element you wish to create, and will default to "div".optionsis an object containing options for creating the new node.htmlis a string that will become the HTML contents of the node.textis a string that will become the text contents of the node.classis an array of classes, or a space seperated string of class names.styleis an object containing style values to set.valueis a string that will become the value of the node.attributesis an object containing attribute values to set.propertiesis an object containing property values to set.datasetis an object containing
7 months ago
7 months ago
1 year ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago