jQuery Next-In-DOM

jQuery Next-In-DOM is a jQuery plugin for getting the next or previous element in the full document matching a specified selector from an element. It basically traverses the whole DOM from the pivot element to find a matching element and returns the same if found. It is like looking vertically up/down from an element on a printed out version of the DOM till you find a match.
Tested on Firefox, Google Chrome and IE7 (and up). If you have any suggestions, comments or if you have found a bug, please send it to vjeleven@gmail.com
Try out the demo below to see it in action. The source for the demo is on the right.
Demonstration
Find the previous matching h2 from this button
Find the next matching a from this button
Reset Highlight
// Find the previous matching h3 from this button
var prevH3 = $(this).prevInDOM('h2');
    
// Find the next matching a from this button
var nextA = $(this).nextInDOM('a');
Download
Download ~ 3 Kb - Last Updated: Jan 9, 2013
GitHub - https://github.com/techfoobar/jquery-next-in-dom
Usage
  1. Include the script on your page:
    <script src="/path/to/nextindom.jquery.min.js"></script>
  2. Once the DOM is ready (or anytime thereafter), you can call the methods:
    
    // get the previous element from the element matching 'selector1' (but not including)
    // regardless of the DOM structure
    var prev1 = $('selector1').prevInDOM();
    
    // get the previous element from 'selector1' matching 'selector2'
    var prev2 = $('selector1').prevInDOM('selector2');
        
    // get the next element from the element matching 'selector1' (but not including)
    // regardless of the DOM structure
    var next1 = $('selector1').nextInDOM();
    
    // get the next element from 'selector1' matching 'selector2'
    var next21 = $('selector1').nextInDOM('selector2');
    
    
Thanks
Thanks to John Resig for creating jQuery. IMHO, it is as world-changing as a JS library can ever expect to become.
Thanks to google-code-prettify for the super-simple-to-use-and-customize syntax highlighter and for the Vim Sunburst theme.
About Me
I am Vijayakrishnan Krishnan. My main area of interest is JavaScript, jQuery, CSS and the awesome things we can achieve with them in combo. I currently work at DeviceDriven with an amazing team of engineers covering JS, CSS on the user side to Groovy on Grails, Spring and Hibernate on the server side.