JSFiddle - React, Tailwind, and code Playground

HTML

<p>foo1</p>
<p id="x">foo2</p>
<h2>bar</h2>
<p>foo3</p>

CSS

.foo { border:1px solid red }

JavaScript

(function($){
  $.fn.wrapConsecutive = function(wrapper){
    if (!this.length) return this;
    var thisRun,prev;
    this.each(function(){
      if (!prev || $(this).prev()[0]!=prev){
        if (thisRun) thisRun.wrapAll(wrapper);
        thisRun = $();
      }
      thisRun = thisRun.add(this);
      prev = this;
    });
    if (thisRun) thisRun.wrapAll(wrapper);
    return this;
  };
})(jQuery);

$('p').wrapConsecutive('<div class="foo"></div>')