
Security News
Crates.io Users Targeted by Phishing Emails
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
@okiba/dom
Advanced tools
Utilities to work with dom elements and selectors
__
npm i --save @okiba/dom
Or import it directly in the browser
<script type="module" src="https://unpkg.com/@okiba/dom/index.js"></script>
import dom from '@okiba/dom'
Okiba Core packages are not transpiled, so don't forget to transpile them with your favourite bundler. For example, using Babel with Webpack, you should prevent imports from okiba to be excluded from transpilation, like follows:
{
test: /\.js$/,
exclude: /node_modules\/(?!(@okiba)\/).*/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
}
}
Selects a DOM Element with a certain id
import {byId} from '@okiba/dom'
const apple = byId('apple')
console.log(apple) // [div.apple]
id
: String
DOM id you are looking for
Element
A DOM Element matching id
Selects a DOM Element, scoped to element
import {qs} from '@okiba/dom'
const pear = qs('.pear')
console.log(pear) // [div.pear]
selector
: String
DOM Selector (tag, class, id, anything that can be passed to querySelector
API)
element
: Element
| optional - default: document
DOM Element to scope the selection query, only childs of that element will be tageted
Element
A DOM Element matching selector
Selects an array of DOM Elements, scoped to element
import {qsa} from '@okiba/dom'
const fruits = qsa('.fruit')
console.log(fruits) // [div.fruit, div.fruit]
selector
: String
DOM Selector (tag, class, id, anything that can be passed to querySelector
API)
element
: Element
| optional - default: document
DOM Element to scope the selection query, only childs of that element will be tageted
Array.<Element>
An array of DOM elements matching selector
Attaches an event listener to a DOM Element, or an array of.
import {qsa, on} from '@okiba/dom'
const buttons = qsa('.button')
on(buttons, 'click', onClick)
on(buttons, ['mouseenter', 'mouseleve'], onMouseChange)
// adds `onClick` to 'click' and `onMouseChange` to both 'mouseenter' and 'mouseleave'
on(buttons, ['click', mouseenter', 'mouseleve'], [onClick, onMouseChange])
window
: Element
or Array.<Element>
| optionalsource the element which will trigger the event
type
: String
or Array.<String>
the event name to bind. Or an array of
handler
: function
or Array.<function()>
the callback to be fired at the event. If an array is supplied the handlers will be bound in order, if there are less handlers than event types, the last handler is bound to all remaining events.
Boolean
Success of the binding
Detached an event listener from a DOM Element, or an array of.
import {qs, off} from '@okiba/dom'
const button = qs('.button')
button.addEventListener('click', onButtonClick)
// or okiba's `on` on(button, 'click')
off(button, 'click', onButtonClick)
// removes `onMouseChange` from both 'mouseenter' and 'mouseleave'
off(buttons, ['mouseenter', 'mouseleve'], onMouseChange)
// removes `onClick` from 'click' and `onMouseChange` from both 'mouseenter' and 'mouseleave'
off(buttons, ['click', mouseenter', 'mouseleve'], [onClick, onMouseChange])
window
: Element
or Array.<Element>
| optionalsource Element which will trigger the event
type
: String
or Array.<String>
Event name to unbind. Or an array of
handler
: function
or Array.<function()>
Callback bound to the event. If an array is supplied the handlers will be unbound in order, if there are less handlers than event types, the last handler is unbound from all remaining events.
Boolean
Success of the unbinding
Read mouse and touch position in the same way
import {eventCoords, on} from '@okiba/dom'
on(window, ['mousemove', 'touchmove'], onMove)
function onMove(e){
const coords = eventCoords(e)
console.log(coords)
}
DOM
: Event
Event
Object
Event position coordinates (clientX and ClientY)
Gets top and left offsets of an element
import {qs, offset} from '@okiba/dom'
const el = qs('.something')
const offsets = offset(el)
console.log(offsets) // Logs: {top: 100, left: 100}
el
: Element
The element you want to get offsets of
Object
Object containing top
and left
offsets
Useful to normalize parameters accepted by modules which work with dom nodes. If you need to have an array of Elements and you want to accept any of: String, String array, Element, Element array
import {qs, getElements} from '@okiba/dom'
const els1 = getElements(['.some', '#thing']) // => [div.some, span#it]
const el = qs('.element')
const els2 = getElements(el) // => [el]
target
: String
or Array.<String>
or Element
or Array.<Element>
The target you want to be sure to obtain as an array of Elements
Array.<Element>
An array of Elements
Checks if an element matches at least one in a list of selectors.
import {matches} from '@okiba/dom'
const isInternal = !!matches(a, '.internal')
//...
const match = matches(myDiv, ['.red', '.green', '.blue])
myDiv.style.backgroundColor = match.replace('.', '')
el
: Element
Element to check
selectors
: String
or Array
Selector (ora array thereof) which the element should match
testAncestors
: Boolean
If true, extends match test upward in the ancestors
String
First matching selector, null
if there was no match
Check if a given element is child of another. The target to match can be an element, selector, or array of selectors.
import {isChildOf} from '@okiba/dom'
const isChildOfAnchor = isChildOf(myNode, 'a')
//... or
const isInsideButton = isChildOf(myNode, myButton)
el
: Element
Element to check
target
: Element
or String
or Array.<String>
Selector to match or Element checked for parent relationship
Boolean
Boolean of match found
Delegate an event callback. It will be executed only if the event target has an ancestor which matches the given target
import {delegate} from '@okiba/dom'
const undelegate = delegate('a.internal-navigation', 'click', onNavigationClick, {capture: true})
function disableNavigation() {
undelegate()
}
target
: String
or Element
Selector or Element to match
event
: String
Event to bind to
callback
: function
Function to be executed at match
options
: Object
or Boolean
Options to be to on
context
: Window
or HTMLDocument
or HTMLElement
Delegation root element
function
Function to be called to remove the delegated callback
Custom event factory. Creates a cross-browsers compatible custom event instance
import {createCustomEvent} from '@okiba/dom'
const enemy = document.getElementById('enemy')
const shinobiAttack = createCustomEvent('shinobi-attack', {
detail: { damage: 3 }
})
enemy.setAttribute('data-life-points', 100)
enemy.addEventListener('shinobi-attack', e => {
const currentLifePoints = enemy.getAttribute('data-life-points')
const updatedlifePoints = Math.max(0, currentLifePoints - e.detail.damage)
enemy.setAttribute('data-life-points', updatedlifePoints)
})
enemy.dispatchEvent(shinobiAttack)
console.log(enemy.getAttribute('data-life-points')) // Logs: 97
type
: String
The custom event type
options
: Object
The custom event options
CustomEvent
The custom event instance
FAQs
DOM helpers for okiba.js
We found that @okiba/dom demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
Product
Socket now lets you customize pull request alert headers, helping security teams share clear guidance right in PRs to speed reviews and reduce back-and-forth.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.