DOM finding

This demo covers DOM finding and traversal helpers that build on top of the basic selector helpers. The basic tp(), tp.Select(), and tp.SelectAll() helpers are covered in the main tp demo, so this page focuses on the next layer.

These functions are useful when code receives an inner element from an event and needs to locate a containing element, inspect direct child elements, detect containment, find a text node, or find an element by the ending of its id.

Sample DOM

All buttons inspect the elements below. The visible monospace labels are the actual element ids used by the JavaScript code.

#DomFindingHost
#Order_1001_Card Text node before elements
#Order_1001_Status: open
#Order_1002_Card #Order_1002_Status: closed

Closest and containment

tp.Closest() locates an ancestor matching a selector. tp.ContainsElement() and tp.ContainsEventTarget() test whether an element owns another element or event target.

Test input: start from #Order_1001_ActionIcon, then find its .dom-finding-card ancestor and check whether #DomFindingHost contains it.

Children and indexes

tp.ChildHTMLElements() returns only direct HTMLElement children. tp.ChildIndex() returns the child element index inside its parent.

Test input: inspect the direct children of #DomFindingHost, then ask for the position of #Order_1002_Card inside that host.

Text node and id ending

tp.FindTextNode() returns the first direct text node. tp.FindElementWithIdEnding() searches a parent for an element whose id ends with the specified text.

Test input: inspect #Order_1001_Status for its first direct text node, then search #DomFindingHost for ids ending in _Status and _ActionIcon.