back to Overview

.up()

Finds the closest parents matching the given selector or filter function for each list element, and returns the results as a list.
Web module only.

Syntax Variants

list.up()
list.up(selector)
list.up(filterFunc)
list.up(selector, parentNum)
list.up(filterFunc, parentNum)

Parameters

selector (optional)
any selector valid for $(), including CSS selectors and lists.
Selectors are optimized for '*', '.classname', 'tagname' and 'tagname.classname'. The performance for other selectors is relative to the number of matches for the selector in the document. Default is '*', which includes all elements.
filterFunc
a function(node) returning true for those nodes that match.
maxParents
maximum number of parents to return per list element. Default is 1.
(return value)
the new list that contains matching parent elements. Duplicate nodes will be automatically removed.

Description

Finds the closest parents matching the given selector or filter function for each list element, and returns the results as a list.

up(selector) is just a shortcut for trav('parentNode', selector, parentNum). up() uses trav() to traverse the DOM tree using parentNode for each list element, until it either finds a matching element or the tree's root has been reached. All matches will added to the result list, at most one for each item in the original list. The result list is filtered to include only unique elements.

Instead of the selector, you can also specify a function that evaluates whether an element matches.

Example

Returns the immediate parent of a node:

var parent = $('#child').up();

Example

Returns all table elements that the list elements are directly contained in.

var tables = $('td.selected').up('table');

Example

Returns a list of all direct parent nodes that have a class that starts with 'special':

var specialParents = $('.myElements').up(function(node) {
    return /(^|\s)special/.test(node.className);
});

See also..

  • trav() allows you to match more than one element. You can also select other relatives such as siblings or children.

Comments

comments powered by Disqus

back to Overview

Functions