Test case details

Preparation Code

<div id="section">  <div>   <div>    <div>     <div>      <div id="end" class="end">       <div>        <div>         <div>          <div>           <div>            <div>             <div>              <div>               <div id="article">                <p>                 <span id="start" class="start"></span>                </p>               </div>              </div>             </div>            </div>           </div>          </div>         </div>        </div>       </div>      </div>     </div>    </div>   </div>  </div> </div> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
var startJQ = $( '#start' ),         startDOM = $( '#start' )[0];         Element.prototype.closestByClassName = function ( className ) {             return this.className && this.className.split( ' ' ).indexOf( className ) > -1             ? this             : ( this.parentNode.closestByClassName && this.parentNode.closestByClassName( className ) );         };

Test cases

Test #1

var element = startJQ.closest( '.start' ),     element = startJQ.closest( '.end' ),     element = startJQ.closest( '.not-found' );  

Test #2

var element = startDOM.closestByClassName( 'start' ),     element = startDOM.closestByClassName( 'end' ),     element = startDOM.closestByClassName( 'not-found' );