or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

attributes.mdcore-selection.mdcss.mddata.mddimensions.mdeffects.mdevents.mdforms.mdindex.mdmanipulation.mdtraversal.mdutilities.md
tile.json

traversal.mddocs/

DOM Traversal

DOM tree traversal methods for navigating element relationships and finding related elements. All methods return new Cash collections for chaining.

Capabilities

Parent Navigation

Navigate up the DOM tree to find parent and ancestor elements.

/**
 * Get direct parent elements, optionally filtered by selector
 * @param comparator - Optional selector, element, or collection to filter parents
 * @returns New Cash collection containing parent elements
 */
parent(comparator?: Comparator): Cash;

/**
 * Get all ancestor elements, optionally filtered by selector
 * @param comparator - Optional selector, element, or collection to filter ancestors  
 * @returns New Cash collection containing ancestor elements
 */
parents(comparator?: Comparator): Cash;

/**
 * Get ancestors until a condition is met, optionally filtered
 * @param until - Selector to stop at (element matching this won't be included)
 * @param comparator - Optional selector to filter the returned ancestors
 * @returns New Cash collection containing ancestors up to the until condition
 */
parentsUntil(until?: Comparator, comparator?: Comparator): Cash;

/**
 * Get the closest matching ancestor element (including self)
 * @param comparator - Selector, element, or collection to match against
 * @returns New Cash collection containing the closest matching ancestor
 */
closest(comparator?: Comparator): Cash;

Usage Examples:

// Get direct parents
const parents = $('.item').parent();
const listParents = $('.list-item').parent('ul');

// Get all ancestors
const allAncestors = $('.nested-element').parents();
const containerAncestors = $('.element').parents('.container');

// Get ancestors until condition
const upToSection = $('.paragraph').parentsUntil('section');
const filtered = $('.element').parentsUntil('.boundary', '.wrapper');

// Find closest matching ancestor
const card = $('.card-content').closest('.card');
const form = $('.input').closest('form');
const modal = $('.close-button').closest('.modal');

// Navigation patterns
$('.breadcrumb-item').on('click', function() {
  const container = $(this).closest('.breadcrumb-container');
  container.find('.active').removeClass('active');
  $(this).addClass('active');
});

// Event delegation with traversal
$('.card').on('click', '.delete-button', function() {
  $(this).closest('.card').fadeOut(() => {
    $(this).remove();
  });
});

Child Navigation

Navigate down the DOM tree to find child and descendant elements.

/**
 * Get direct child elements, optionally filtered by selector
 * @param comparator - Optional selector, element, or collection to filter children
 * @returns New Cash collection containing child elements
 */
children(comparator?: Comparator): Cash;

/**
 * Get all child nodes including text and comment nodes
 * @returns New Cash collection containing all child nodes
 */
contents(): Cash;

/**
 * Find descendant elements matching selector
 * @param selector - CSS selector to search for
 * @returns New Cash collection containing matching descendants
 */
find(selector: string): Cash;

/**
 * Filter elements that have descendants matching selector or element
 * @param selector - Selector string or DOM node to check for
 * @returns New Cash collection containing elements that have matching descendants
 */
has(selector: string | Node): Cash;

Usage Examples:

// Get all direct children
const allChildren = $('.container').children();
const listItems = $('.nav').children('li');

// Get all child nodes (including text)
const allNodes = $('.content').contents();
const textNodes = $('.paragraph').contents().filter(function() {
  return this.nodeType === 3; // Text nodes
});

// Find descendants
const links = $('.navigation').find('a');
const inputs = $('.form').find('input, textarea, select');

// Filter by having descendants
const withImages = $('.post').has('img');
const withRequired = $('.form').has('[required]');

// Complex traversal
$('.section').children('.item').find('.title').addClass('styled');

// Working with iframe contents
const iframeBody = $('iframe').contents().find('body');
iframeBody.css('font-family', 'Arial, sans-serif');

Sibling Navigation

Navigate horizontally through sibling elements.

/**
 * Get immediately following sibling elements
 * @param comparator - Optional selector, element, or collection to filter siblings
 * @returns New Cash collection containing next sibling elements
 */
next(comparator?: Comparator): Cash;

/**
 * Get all following sibling elements
 * @param comparator - Optional selector, element, or collection to filter siblings
 * @returns New Cash collection containing all following sibling elements
 */
nextAll(comparator?: Comparator): Cash;

/**
 * Get following siblings until a condition is met
 * @param until - Selector to stop at (element matching this won't be included)
 * @param comparator - Optional selector to filter the returned siblings
 * @returns New Cash collection containing following siblings up to the until condition
 */
nextUntil(until?: Comparator, comparator?: Comparator): Cash;

/**
 * Get immediately preceding sibling elements
 * @param comparator - Optional selector, element, or collection to filter siblings
 * @returns New Cash collection containing previous sibling elements
 */
prev(comparator?: Comparator): Cash;

/**
 * Get all preceding sibling elements
 * @param comparator - Optional selector, element, or collection to filter siblings
 * @returns New Cash collection containing all preceding sibling elements
 */
prevAll(comparator?: Comparator): Cash;

/**
 * Get preceding siblings until a condition is met
 * @param until - Selector to stop at (element matching this won't be included)
 * @param comparator - Optional selector to filter the returned siblings
 * @returns New Cash collection containing preceding siblings up to the until condition
 */
prevUntil(until?: Comparator, comparator?: Comparator): Cash;

/**
 * Get all sibling elements (both preceding and following)
 * @param comparator - Optional selector, element, or collection to filter siblings
 * @returns New Cash collection containing sibling elements
 */
siblings(comparator?: Comparator): Cash;

Usage Examples:

// Navigate to adjacent siblings
const nextItem = $('.current').next();
const prevButton = $('.active-button').prev('button');

// Get all siblings in direction
const followingSiblings = $('.marker').nextAll();
const precedingSiblings = $('.marker').prevAll('.item');

// Get siblings until condition
const itemsUntilSeparator = $('.start').nextUntil('.separator');
const groupItems = $('.item').prevUntil('.group-header', '.item');

// Get all siblings
const allSiblings = $('.selected').siblings();
const siblingButtons = $('.current-button').siblings('button');

// Tab navigation example
$('.tab').on('click', function() {
  const index = $(this).index();
  $(this).addClass('active').siblings().removeClass('active');
  $('.tab-panel').eq(index).show().siblings().hide();
});

// List manipulation
$('.list-item').on('click', '.move-up', function() {
  const item = $(this).closest('.list-item');
  const prev = item.prev('.list-item');
  if (prev.length) {
    item.insertBefore(prev);
  }
});

// Group operations
$('.group-header').next().addClass('first-in-group');
$('.section-divider').nextUntil('.section-divider').wrapAll('<div class="section"></div>');

Collection Testing and Filtering

Test elements against selectors and filter collections.

/**
 * Test if any element matches the comparator
 * @param comparator - Selector, element, collection, or predicate function to test against
 * @returns True if any element matches, false otherwise
 */
is(comparator?: Comparator): boolean;

/**
 * Filter out elements that match the comparator
 * @param comparator - Selector, element, collection, or predicate function to exclude
 * @returns New Cash collection with non-matching elements
 */
not(comparator?: Comparator): Cash;

Usage Examples:

// Test against selectors
const isVisible = $('.element').is(':visible');
const isForm = $('.container').is('form');
const hasClass = $('.item').is('.active');

// Test with predicate function
const isLarge = $('.box').is(function() {
  return this.offsetWidth > 200;
});

// Filter out elements
const notHidden = $('.items').not(':hidden');
const notActive = $('.buttons').not('.active');
const notFirst = $('.items').not(':first-child');

// Filter with function
const notEmpty = $('.containers').not(function() {
  return $(this).children().length === 0;
});

// Conditional operations based on tests
if ($('.form').is(':valid')) {
  $('.submit-button').prop('disabled', false);
}

// Complex filtering
const validInputs = $('.form-field')
  .not('.disabled')
  .not('[readonly]')
  .filter(':visible');

// Test and branch logic
$('.item').each(function() {
  if ($(this).is('.special')) {
    $(this).addClass('highlight');
  } else {
    $(this).removeClass('highlight');
  }
});

Traversal Patterns

Finding Related Elements

// Find labels for inputs
$('.form-input').each(function() {
  const label = $('label[for="' + this.id + '"]');
  if (label.length === 0) {
    // Create label if missing
    $(this).before(`<label for="${this.id}">${this.name}</label>`);
  }
});

// Navigate table structures
$('.data-table tbody tr').on('click', function() {
  const cells = $(this).children('td');
  const headers = $(this).closest('table').find('thead th');
  // Process row data...
});

Building Navigation

// Create breadcrumb navigation
function buildBreadcrumbs(element) {
  const breadcrumbs = [];
  $(element).parents('[data-title]').each(function() {
    breadcrumbs.unshift($(this).data('title'));
  });
  return breadcrumbs;
}

// Accordion behavior
$('.accordion-header').on('click', function() {
  const content = $(this).next('.accordion-content');
  const isOpen = content.is(':visible');
  
  // Close other sections
  $(this).siblings('.accordion-header').next().hide();
  
  // Toggle current section
  content.toggle(!isOpen);
});

Performance Tips

  • Cache frequently used selectors
  • Use specific selectors to limit traversal scope
  • Prefer children() over find() when you only need direct children
  • Use has() to filter instead of find().length > 0
// Less efficient
$('.container').find('*').filter('.target');

// More efficient
$('.container').find('.target');

// Even more efficient if you know the structure
$('.container').children('.section').children('.target');