You can find the next sibling of the img
elements and see whether it is an non-empty text node, if so then wrap it with a p
node using .wrap()
$('p img').each(function () {
var next = this.nextSibling;
if (next && next.nodeType == Node.TEXT_NODE && $.trim(next.nodeValue) != '') {
$(next).wrap('<p/>')
}
})
Demo: Fiddle