Package Exports
- base-elem-js
- base-elem-js/dist/js/base-elem-js.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (base-elem-js) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Base Elem Js
base-elem-js is a lightweight utility for DOM manipulation, including querying elements, adding/removing classes, setting attributes, and handling events. The minified package comes in at 4.67kb! It is designed to work with a collection of elements or a single element.
Usage
To use the base-elem-js utility, you need to import it as follows:
import $be from 'base-elem-js;Base Elem Methods ($be().*)
find(selector: string, filter?: (elem: any, i: number) => boolean): BaseElemFinds elements matching the selector within the current elements and returns a new BaseElem instance.
findBy(type: FindBy, selector: string, filter?: (elem: any, i: number) => boolean): BaseElemFinds elements by type (id, class, or tag) within the current elements and returns a new BaseElem instance.
findOne(selector: string): BaseElemFinds the first element matching the selector within the current elements and returns a new BaseElem instance.
each(fn: (elem: HTMLElement, i: number) => void): BaseElemIterates over each element and applies the provided function.
css(attrs: Partial<CSSProperties> | string): BaseElemSets or gets CSS properties for the current elements.
addClass(cssNames: string | string[]): BaseElemAdds the specified class(es) to the current elements.
rmClass(cssNames: string | string[]): BaseElemRemoves the specified class(es) from the current elements.
tgClass(cssNames: string | string[], toggle?: boolean): BaseElemToggles the specified class(es) on the current elements.
hasClass(cssNames: string | string[], method: 'some' | 'every' = 'some'): booleanChecks if the current elements have the specified class(es).
attr(attrs: Record<string, string> | string): BaseElemSets or gets attributes for the current elements.
empty(): BaseElemEmpties the content of the current elements.
remove(): BaseElemRemoves the current elements from the DOM.
insert(html: string | HTMLElement | HTMLElement[], method: AppendMethod = 'append'): BaseElemInserts HTML or elements into the current elements using the specified method (append, prepend, after, before).
html(html: string): BaseElemSets the inner HTML of the current elements.
text(text: string): BaseElemSets the inner text of the current elements.
on(evtName: `${Event['type']}.${string}` | string, fn: EventFn, delegateEl: string = null, config: boolean | AddEventListenerOptions = false): BaseElemAdds an event listener to the current elements. Namespace the events with a '.', for example click.myClickName.
off(evtName: string, config: boolean | AddEventListenerOptions = false): BaseElemRemoves an event listener from the current elements. Pass in the same string value as the 'on' method. Namespace with '.', or click.myClickName as the function name.
Examples
import $be from './base-elem-js';
const bes = $be.static;
$be('h1').css({color: 'green'});
const $hidden = $be('.hidden').css({display: 'block'}).attr({hidden: null});
console.log(bes.isHidden($hidden.elem[0]));
console.log('has "what" class',$be('ul').find('li').hasClass('what'));
const div = bes.make('div', {id: 'test', className: 'test'}, '<h2>Hello Make!</h2><p>Some copy goes here</p>');
const $div = $be(div);
$div.on('click', (ev, elem) => {
console.log('clicked', elem.textContent);
},'h2');
$be(document.body).insert(div);
$div.insert('<p>Some more copy</p>', 'before');
$div.insert('<p>Copy Prepended</p>', 'prepend');Base Elem Static ($be.static)
make(tag: string, attrs?: Record<string, any>, html?: string): HTMLElementCreates a new HTML element with the specified tag, attributes, and inner HTML content.
isHidden(elem: HTMLElement): booleanChecks if the specified element is hidden (i.e., has display: none or visibility: hidden).
isVisible(elem: HTMLElement): booleanChecks if the specified element is visible (i.e., does not have display: none or visibility: hidden).
find(selector: string, base: HTMLElement = document): HTMLElement[]Finds elements matching the selector within the specified base element.
findBy(type: FindBy, selector: string, base: HTMLElement = document): HTMLElement[]Finds elements by type (id, class, or tag) within the specified base element.
findOne(selector: string, base: HTMLElement = document): HTMLElementFinds the first element matching the selector within the specified base element.
addClass(elem: HTMLElement, cssNames: string | string[]): voidAdds the specified class(es) to the element.
rmClass(elem: HTMLElement, cssNames: string | string[]): voidRemoves the specified class(es) from the element.
tgClass(elem: HTMLElement, cssNames: string | string[], toggle?: boolean): voidToggles the specified class(es) on the element.
hasClass(elem: HTMLElement, cssNames: string | string[], method: 'some' | 'every' = 'some'): booleanChecks if the element has the specified class(es).
attr(elem: HTMLElement, attrs: Record<string, string> | string): voidSets or gets attributes for the element.
empty(elem: HTMLElement): voidEmpties the content of the element.
remove(elem: HTMLElement): voidRemoves the element from the DOM.
insert(elem: HTMLElement, html: string | HTMLElement | HTMLElement[], method: AppendMethod = 'append'): voidInserts HTML or elements into the specified element using the specified method (append, prepend, after, before).
html(elem: HTMLElement, html: string): voidSets the inner HTML of the element.
text(elem: HTMLElement, text: string): voidSets the inner text of the element.
Base Element Static
import $bs from './core/BaseStatic';
const bes = $bs.static;
const div = bes.make('div', { id: 'test', className: 'test' }, '<h2>Hello Make!</h2><p>Some copy goes here</p>');
document.body.appendChild(div);
bes.addClass(div, 'new-class');
bes.rmClass(div, 'test');
bes.tgClass(div, 'toggle-class');
console.log(bes.hasClass(div, 'new-class'));
bes.attr(div, { 'data-test': 'value' });
console.log(bes.attr(div, 'data-test'));
bes.empty(div);
bes.html(div, '<p>New content</p>');
bes.text(div, 'New text');
bes.insert(div, '<p>Inserted content</p>', 'before');