Returns a reference to an element by its ID.
import { byId } from 'tsumami';
const content = byId('main-content');
See: https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById
string
id - ID string
Element
Returns an array of all child elements which have all of the given class names
import { byClassName } from 'tsumami';
const listItems = byClassName('list__items');
string
className - class string to search forElement|Document
[ctx=document] - Root element.document
by default
Array
Returns the first element within the document that matches the specified group of selectors
import { qs } from 'tsumami';
const content = qs('#main-content');
See: https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector
string
selector - CSS selectorElement|Document
[ctx=document] - Root element.document
by default
Element
Returns a list of the elements within the document that match the specified group of selectors
import { qsa } from 'tsumami';
const listItems = qsa('.list .list-items');
See: https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll
string
selector - One or more CSS selectors separated by commas.Element|Document
[ctx=document] - Root element.document
by default
Array
Returns a parsed data attribute from the passed-in node. If not found returns null
import { byId, data } from 'tsumami';
//html: <div id="content" data-name="my-content" data-idx="1" data-bool="false"></div>
const content = byId('content');
const name = data(content, 'name'); // "my-content"
const idx = data(content, 'idx'); // 1
const bool = data(content, 'bool'); // false
Element
element - DOM Elementstring
[attr] - Data attribute to retrieve (without thedata-
). If empty will return an object with everydata-
attribute as key.
Converts passed-in Element or NodeList to an array.
import { toArray } from 'tsumami';
const content = document.getElementById('content');
const arrayLike = document.querySelectorAll('.elements');
const elements = toArray(arrayLike);
// Array.isArray(elements) === true
const contentArray = toArray(content);
// Array.isArray(contentArray) === true
array|Element|NodeList
element - Value to convert
array
Returns true
if element
would be selected by the specified selector
string; otherwise, returns false.
import { matches, qs } from 'tsumami';
const el = qs('.parent .child');
if (matches(el, '.parent')) {
// false
}
if (matches(el, '.parent .child')) {
// true
}
Element
elementstring
selector
boolean
Gets the ancestors of an element, optionally filtered by a selector.
import { parents, qs } from 'tsumami';
const listItem = qs('li.list-item');
const parentsEls = parents(listItem);
const parentLists = parents(listItem, 'ul');
Element
element - Source elementstring
[selector] - A string containing a selector expression to match elements against.
Array
Gets the first element that matches selector
by testing the element itself and traversing up through its ancestors in the DOM tree.
Returns null
if nothing matches.
Will use native Element.prototype.closest
if available.
import { closest, qs } from 'tsumami';
const listItem = qs('li.list-item');
const list = closest(listItem, 'ul');
Element
element - Source elementstring
selector - A string containing a CSS selector expression to match
Adds a new class to the element
import { addClass, byId } from 'tsumami';
const content = byId('content');
addClass(content, 'new-class');
Element
element - Target elementstring
className - Class to add
Removes a class from the element
import { removeClass, byId } from 'tsumami';
const content = byId('content');
removeClass(content, 'remove-me');
Element
element - Target elementstring
className - Class to remove
Checks if an element has a given class
import { hasClass, byId } from 'tsumami';
const content = byId('content');
if (hasClass(content, 'remove-me')) {
//...
}
Element
element - Target elementstring
className - Class to check
Toggle a class on the element.
If class exists then removes it, if not, then adds it.
When the second argument is present and is true
, adds specified class value, if is false
removes it.
import { toggleClass, byId } from 'tsumami';
// html: <div id="content"></div>
const content = byId('content');
toggleClass(content, 'random-class')
// html: <div id="content" class="random-class"></div>
toggleClass(content, 'random-class')
// html: <div id="content"></div>
toggleClass(content, 'random-class')
// html: <div id="content" class="random-class"></div>
toggleClass(content, 'random-class', true)
// html: <div id="content" class="random-class"></div>
Element
element - Target elementstring
className - Class to toggleboolean
[toggle] - Force add or removal of the class