Chainable jQuery-like list of nodes
import Nodes from 'tsumami/lib/nodes';
const els = new Nodes('.my-class');
string|array|NodeList|Element
elements - A CSS string, list or array of elementsElement|Document
[ctx=document] - Root element. Defaults todocument
Constructor
The number of elements in the list
import Nodes from 'tsumami/lib/nodes';
const els = new Nodes('.my-class');
console.log(els.length); // number...
number
Returns a shallow copy array of elements in the set
import Nodes from 'tsumami/lib/nodes';
const els = new Nodes('.my-class');
const array = els.toArray();
Array
Returns an element at the given index or undefined
import Nodes from 'tsumami/lib/nodes';
const els = new Nodes('.my-class');
const firstElement = els.eq(0);
number
index - Element index
Element
Iterates iterator
function on every element in the set
import Nodes from 'tsumami/lib/nodes';
import { addClass } from 'tsumami';
const els = new Nodes('.my-class');
els.forEach((el, index) => addClass(el, `element-${index}`));
function
iterator - Iterator function
Nodes
Returns the index of an element in the current list or -1
if not found
import Nodes from 'tsumami/lib/nodes';
import { qs } from 'tsumami';
const els = new Nodes('.my-class');
const firstElement = qs('.my-class');
els.indexOf(firstElement) // === 0;
Element
target - Target element
number
Gets or sets and attribute on the set of elements
import Nodes from 'tsumami/lib/nodes';
const els = new Nodes('.my-class');
//get the id of the first element in the set
const id = els.attr('id');
//set as string
els.attr('data-str', 'a random string');
//set as function
els.attr('data-str', (el, index) => `position-${index}`);
string
attr - Attribute to set- [value] - Value to set. If a function, it will receive every element and its index as arguments
Nodes
Adds a class to the elements
import Nodes from 'tsumami/lib/nodes';
const els = new Nodes('.my-class');
//set as string
els.addClass('another-class');
//set as function
els.addClass((el, index) => (index === 0 ? 'is-first' : ''));
string|function
className - CSS class to add or function returning the class string (signature:(element, index) => {}
)
Nodes
Removes a class from the elements
import Nodes from 'tsumami/lib/nodes';
const els = new Nodes('.my-class');
//set as string
els.removeClass('another-class');
//set as function
els.removeClass((el, index) => (index === 0 ? 'is-first' : ''));
string|function
className - CSS class to remove or function returning the class string (signature:(element, index) => {}
)
Nodes
Toggles a class on the elements
import Nodes from 'tsumami/lib/nodes';
const els = new Nodes('.my-class');
//set as string
els.toggleClass('is-active');
// force add
els.toggleClass('is-active', true);
// dynamic class name
els.toggleClass((el, index) => `element-${index}`);
// dynamic toggle flag
els.toggleClass('is-active', (el, index) => index === 0); //added just to the first element
string|function
className - CSS class to toggle or function returning the class string (signature:(element, index) => {}
)boolean|function
[toggle] - Force add or removal of the class or function returning a boolean (signature:(element, index) => {}
)
Nodes