JSFiddle - React, Tailwind, and code Playground

HTML

<p id="a">foo1</p>
<p id="b">foo2</p>
<h2>bar</h2>
<p id="c">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);

$()
  .add($('#b')).add($('#c')).add($('#a'))
  .wrapConsecutive('<div class="foo"></div>');