UFHtml implements methods for supporting html and the dom.

Properties

s_escapeHtmlMap: Map<string, string> = ...

Maps certain characters to their entity or special html tag or empty string if it has no use in html

Methods

  • Parameters

    • element: Element

      Element to add and remove the classes to and from; can be null, in that case nothing happens.

    • addClasses: string

      Css classes separated by a space character; can be null, in that case no classes are added.

    • removeClasses: string

      Css classes separated by a space character; can be null, in that case no classes are removed.

    Returns void

  • Adds a listener to the body element for one or more events. If the target or any of the parents of the target matches the selector, the listener is called. The function returns a callback, which can be called to remove the listener. This method can be used to handle events fired by elements that are dynamically added at a later time.

    Type Parameters

    • T extends HTMLElement

    Parameters

    • selector: string

      Selector the target must match.

    • events: string

      One or more events to add listener for (separated by space)

    • handlerFactory: (element: T) => EventListenerOrEventListenerObject

      A factory function that creates a handler callback for the element. Note that this function is called everytime an event is fired. The function should take as little time as possible.

    Returns UFCallback

    a function that can be called to remove the listener from the body.

  • Adds css classes in a single string to an element.

    Parameters

    • element: null | Element

      Element to add the classes to; can be null, in that case nothing happens.

    • classes: null | string

      Css classes separated by a space character; can be null, in that case nothing happens.

    Returns void

  • Adds a listener for one or more events. The function returns a callback, which can be called to remove the listener.

    Parameters

    • element: string | Document | HTMLElement | Window

      Element to add listener to or selector for the element

    • events: string

      One or more events to add listener for (separated by space)

    • listener: EventListenerOrEventListenerObject

      Listener callback

    Returns UFCallback

    a function that can be called to remove the listener from the element for the events.

  • Adds a listener for one or more events to an element or a list of elements. The function returns a callback, which can be called to remove all the listener.

    Type Parameters

    • T extends HTMLElement

    Parameters

    • selector: string | NodeListOf<T> | T[]

      Selector for the element(s) or a list of elements.

    • events: string

      One or more events to add listener for (separated by space).

    • handlerFactory: (element: T) => EventListenerOrEventListenerObject

      A factory function that creates a handler callback for the element.

    Returns UFCallback

    a function that can be called to remove all the added listener from the elements for the events.

    // without event
    const removeListeners = UFHtml.addListeners(
    '.some-button',
    'click touchstart',
    (element) => () => {
    // do something with element
    }
    );
    // with event
    const removeListeners = UFHtml.addListeners(
    '.some-button',
    'click touchstart',
    (element) => (event) => {
    // do something with element and event
    }
    );
  • Assigns a value to a form field element and triggers the "input" and "change" events.

    With checkbox/radio elements the following values will set the checked state to true: 'true', '1', 'checked'. Any other value will set the checked state to false.

    If the element is not a form field element, nothing happens.

    Parameters

    • element: Element

      Element to assign to

    • value: string

      Value to assign

    Returns boolean

    true if the value could be assigned, false if the element is not a form field.

  • Builds a map of data attributes from the element. The method will skip data attributes that start with 'data-uf-'.

    The result can be used with UFHtml.copyAttributes.

    Parameters

    • element: HTMLElement

      Element to get data attributes from.

    Returns { [key: string]: string }

    an object where the keys are the attribute names and the values are the attribute enclosed by square brackets.

    // <button id="foobar" data-foo data-bar></button>
    const element = document.getElementById('foobar');
    const map = UFHtml.buildDataAttributesMap(element);
    // map is { 'data-foo': '[data-foo]', 'data-bar': '[data-bar]' }
  • Copies one or more attribute values to elements. Depending on the type of the element the value gets handled as follows:

    • input: the checked or value property is set (depending on the type).
    • textarea: the value property is set.
    • select: the value property is set.
    • img: the src property is set.
    • any other element: the inner text of the element is set.

    Parameters

    • element: Element

      Element to get the attributes from

    • map: { [key: string]: string }

      The field names are used as attribute names and the values are used as selectors for the target elements. If the selector points to multiple elements, each element will get the attribute value.

    • Optionalcontainer: DocumentFragment | Element

      Container to search the target elements in; if not set, the document is used.

    Returns void

  • Creates an element by parsing a piece of html.

    Type Parameters

    • T extends Element

    Parameters

    • html: string

      Html to parse

    Returns T

    created element; the element is removed from the document before it is returned.

  • Removes all child elements from an element.

    Parameters

    • element: Element

      Element to remove all children of.

    Returns void

  • Converts plain text to html by replacing certain characters with their entity equivalent and replacing \n with
    tags.

    Based on code from answer: https://stackoverflow.com/a/4835406/968451

    Parameters

    • text: string

      Text to convert

    Returns string

    Html formatted plain text

  • Fades in an element by setting the styles opacity and transition.

    Parameters

    • element: HTMLElement

      Element to fade in

    • duration: number = 400

      Duration in millisecond for the fade in transition (default = 400)

    Returns void

  • Fades out an element by setting the styles opacity and transition.

    Parameters

    • element: HTMLElement

      Element to fade out

    • duration: number = 400

      Duration in millisecond for the fade in transition (default = 400)

    Returns void

  • Gets all elements for an attribute.

    Type Parameters

    • T extends Element

    Parameters

    • name: string

      Attribute name

    • value: null | string = null

      Attribute value or use null to ignore value

    • Optionalcontainer: Element

      Container to search the element in; if not set, the document is used.

    Returns NodeListOf<T>

    found elements

  • Tries to find an element for an attribute.

    Type Parameters

    • T extends Element

    Parameters

    • name: string

      Attribute name

    • value: null | string = null

      Attribute value or use null to ignore value

    • Optionalcontainer: Element

      Container to search the element in; if not set, the document is used.

    Returns null | T

    found element or null if no element could be found

  • Gets an element for a selector. If the selector is an element, it just returns the element.

    If the selector is a string, it will try to find the element in the document or container.

    If no element can be found or the selector is a null value, the method will throw an error.

    Type Parameters

    • T extends Element

    Parameters

    • selector: null | string | T

      Element, selector text or null

    • Optionalcontainer: Element

      Container to search the element in; if not set, the document is used.

    Returns T

    found element

    Error if no element can be found

  • Gets the value of an attribute.

    Parameters

    • element: HTMLElement

      Element to get attribute from

    • name: string

      Name of attribute

    • defaultValue: string = ''

      Default value to return if no value could be determined (default = '')

    Returns string

    the value of the attribute or aDefault if there is no value.

  • Gets all attribute names of an element.

    Parameters

    • element: HTMLElement

      Element to get the attribute names from

    Returns string[]

    all the names of attributes defined at the element

  • Gets the first parent element of the element that matches the selector.

    Parameters

    • element: HTMLElement

      Element to get the parent (or grandparent or great-grandparent) of

    • selector: string

      Selector to filter the parent with

    Returns null | HTMLElement

    the parent element that matches the selector or null if no parent could be found

    Use built-in Element.closest() instead

  • Gets an element for an attribute.

    If no element can be found the method will throw an error.

    Type Parameters

    • T extends Element

    Parameters

    • name: string

      Attribute name

    • value: null | string = null

      Attribute value or use null to ignore value

    • Optionalcontainer: Element

      Container to search the element in; if not set, the document is used.

    Returns T

    found element

    Error if no element can be found

  • Gets an element for a dom ID and typecast it to a certain type.

    If no element can be found, the method will throw an error.

    Type Parameters

    • T extends Element = HTMLElement

    Parameters

    • id: string

      The dom id of element

    Returns T

    found element

    Error if no element can be found

  • Gets all parents of an element.

    Parameters

    • element: HTMLElement

      Element to get all parents for

    • Optionalselector: string

      Optional selector to filter the parents with

    Returns HTMLElement[]

    all parent elements of the element (parent, grandparent, great-grandparent, etc.)

  • Checks if an element has an attribute.

    Parameters

    • element: HTMLElement

      Element to check attribute for

    • name: string

      Name of attribute

    Returns boolean

    true if the element has the attribute, false if not.

  • Hides an element by updating the display style property. The current value is stored in the element and is used by show. Then the value 'none' is assigned to display style.

    Parameters

    • element: HTMLElement

      Element to hide

    Returns void

  • Inserts an element after another element.

    Parameters

    • parent: Element

      Parent to insert the element in

    • newElement: Element

      Element to insert

    • referenceElement: Element

      Element to insert the new element after

    Returns void

  • Checks if an element is visible, that it is not hidden by some styling and the element has some size.

    Parameters

    • element: HTMLElement

      Element to check

    • checkParent: boolean = true

      True to check the parents of the element as well, false to only check the element itself.

    Returns boolean

    true if the element is visible, false if not. Note that if only element itself is checked, it does not take into account of any parent is not visible.

  • Removes css classes in a single string from an element.

    Parameters

    • element: null | Element

      Element to remove the classes from; can be null, in that case nothing happens.

    • classes: null | string

      Css classes separated by a space character; can be null, in that case nothing happens.

    Returns void

  • Shows a element by updating the display style property.

    Parameters

    • element: HTMLElement

      Element to show

    • Optionaldisplay: string

      When set use this value, else use the initial value which was copied with hide. If there is no initial value, use 'block'.

    Returns void

  • Toggle css classes in a single string in an element.

    Parameters

    • element: null | Element

      Element to add to or remove from the classes; can be null, in that case nothing happens.

    • classes: null | string

      Css classes separated by a space character; can be null, in that case nothing happens.

    • Optionalforce: boolean

      If true the classes are added, if false the classes are removed, if not set the classes are toggled.

    Returns void