Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Web: DOM
Alex Bruno Cáceres edited this page Mar 9, 2023
·
6 revisions
This is a module with some shorthand methods to execute common and useful DOM commands.
Just some short aliases for:
- html:
document.documentElement - head:
document.head - body:
document.body
Shorthand for document.querySelector.
import{$}from'@jsweb/utils/web/dom'constcontainer=$('div.container')// returns the div.container DOM element or undefined if not foundShorthand for document.querySelectorAll.
import{$$}from'@jsweb/utils/web/dom'constitems=$$('li.item')// returns an array of the li.item elements or an empty array if noneShorthand for document.createElement, but with a useful way to add properties to the element.
import{create}from'@jsweb/utils/web/dom'constlink=create('a',{href: 'https://some.cool.site',target: '_blank',class: 'stylish-link'})// returns the created elementNote: Only valid HTML element properties. Don't try to add inner content or event listening here.
Shorthand for Element.appendChild.
import{append,create}from'@jsweb/utils/web/dom'constli=create('li',{class: 'list-item'})li.textContent='New Item'append('ul',li)Shorthand for Element.addEventListener.
import{listen}from'@jsweb/utils/web/dom'listen('#click-here','click',(event)=>{ ... })