Report abuse


			
  up: function(element) {
    if(!arguments[1]) return element.parentNode;
    
    if(typeof arguments[1] == 'number') {
      arguments[1].times(function(){ element = element.parentNode });
      return element;
    }
    
    var filter = arguments[1];
    while((element = element.parentNode) && element.parentNode) {
      var match = true;
      for(key in filter) switch(key) {
        case 'class':
        case 'className':
          if(!$(element).hasClassName(filter[key])) match = false;
          break;
        case 'tag':
        case 'tagName':
          if(element.tagName.toUpperCase()!=filter[key].toUpperCase()) 
            match = false;
          break;
        default:
          if(element[key]!=filter[key]) match = false;
      }
      
      if(match) return element;
    }
  },

// examples

  testElementUp: function() { with(this) {
      assertEqual('Container', $('collect').up().id);
      assertEqual('Container', $('collect').up(1).id);
      assertEqual('Container', $('5').up(2).id);

      assertEqual('Container', $('collect').up({tagName:'DIV'}).id);
      assertEqual('4', $('5').up({className:'thirdClass'}).id);
      
      assertEqual('Container',$('5').up({className:'moo',tagName:'DIV'}).id);
      assertEqual('Container',$('5').up({tag:'div'}).id);
      assertEqual('Container',$('5').up({'class':'moo','tag':'div'}).id);
      assertNull($('5').up({className:'moo',tagName:'IMG'}));
    }},

/* for this HTML

First class Second class First and Second class Third class Nested First class
123
  • 4
*/