JSFiddle - React, Tailwind, and code Playground

by kasdega

HTML

<div class="result">Hello World</div>

JavaScript

jQuery.fn.outerHTML = function(s) {
return (s)
? this.before(s).remove()
: jQuery("<p/>").append(this.eq(0).clone()).html();
}

$(document).ready(function() {
    var text = "some text to replace here.... text text text";
    var element = $('<img src="image">');
    
    $('.result').html(ReplaceWithObject(text, "here", element));
});
    
function ReplaceWithObject(textSource, textToReplace, objectToReplace) {
    var strings = textSource.split(textToReplace);
    if(strings.length >= 2) {
        return strings[0] + objectToReplace.outerHTML() + strings[1];
    }
    return "";
}