code: function closest(e, classname) {
      if ($(e).hasClass(classname)) {
          return e;
      }
      return e.parentNode && closest(e.parentNode, classname);
  }
docs: returns the closest element to 'e' that has class "classname"