DOM Utilities

provides TypeScript methods for manipulating the DOM

Several utilities are provided to make it more convenient to work with the dom.

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import {els, opt, req, setDisplay} from "./dom";

// els<T extends HTMLElement>(selector: string, context?: HTMLElement): readonly T[]
const list1 = els("button"); // find all buttons
const list2 = els("button", someElement); // find all buttons within provided parent

// opt<T extends HTMLElement>(selector: string, context?: HTMLElement): T | undefined
const elOption = opt<HTMLButtonElement>("#my-button"); // returns undefined if not present

// req<T extends HTMLElement>(selector: string, context?: HTMLElement): T
const el = req<HTMLDivElement>("#my-div");

setHTML(el, "<em>Hello!</em>");
setDisplay(el, false);
setText(el, "Hello!");
clear(el);