JSFiddle - React, Tailwind, and code Playground
HTML
<div>
<div id="one">ONE</div>
</div>
<div>Something in the middle</div>
<div>
<div></div>
<div id="two">TWO</div>
</div>
JavaScript
$(function(){
var $one = $("#one");
$one.click(function(){ alert("IT'S ONE!") });
var $two = $("#two");
$two.click(function(){ alert("IT'S TWO!") });
var $onePrev = $one.prev();
if( $onePrev.length < 1 ) var $oneParent = $one.parent();
var $twoPrev = $two.prev();
if( $twoPrev.length < 1 ) var $twoParent = $two.parent();
if( $onePrev.length > 0 ) $onePrev.after( $two );
else $oneParent.prepend( $two );
if( $twoPrev.length > 0 ) $twoPrev.after( $one );
else $twoParent.prepend( $one );
});