Skip to content

Instantly share code, notes, and snippets.

@SvetlozarKalchev
Created November 30, 2016 17:19
Show Gist options
  • Select an option

  • Save SvetlozarKalchev/c4c62fe9a66c19a114e4e18060b293fb to your computer and use it in GitHub Desktop.

Select an option

Save SvetlozarKalchev/c4c62fe9a66c19a114e4e18060b293fb to your computer and use it in GitHub Desktop.

Revisions

  1. SvetlozarKalchev created this gist Nov 30, 2016.
    23 changes: 23 additions & 0 deletions jquery-dir-function.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    define( [
    "../../core"
    ], function( jQuery ) {

    "use strict";

    return function( elem, dir, until ) {
    var matched = [],
    truncate = until !== undefined;
    // elem[dir] is DOMElement.parentNodes in this case.
    // This code goes through all parent Nodes and only pushed the node in the collection if it's a DOM node.
    while ( ( elem = elem[ dir ] ) && elem.nodeType !== 9 ) {
    if ( elem.nodeType === 1 ) {
    if ( truncate && jQuery( elem ).is( until ) ) {
    break;
    }
    matched.push( elem );
    }
    }
    return matched;
    };

    } );