JSFiddle - React, Tailwind, and code Playground
by Matt Ball
HTML
<div>
<p>Hello</p>
<div>
<p>Foo</p>
<p class="bar">Bar</p>
<p>rawr!</p>
</div>
<p>bop</p>
<input/>
<div>
<p>Another</p>
</div>
</div>
<p>last</p>
JavaScript
// open your console
;(function ($)
{
$.fn.nextInDocument = function (s)
{
var self = this,
next;
do
{
next = self.nextAll(s).first();
self = self.parent();
}
while (!next.length && self.length);
return next;
};
})(jQuery);
var rawr = $('.bar').nextInDocument('p'),
bop = rawr.nextInDocument('p');
console.log(rawr); // <p>rawr</p>
console.log(bop); // <p>bop</p>