jQuery('.page div.table>table').each(function () { let $table = jQuery(this); let $prevDiv = $table.parent().prev(); if ($prevDiv.hasClass('wrap_caption') || $prevDiv.hasClass('caption')) { if ($prevDiv.children('p').length > 0) { let captionText = $prevDiv.children('p').html(); $table.prepend('' + captionText + ''); $prevDiv.remove(); if (location.hash) { location.href = location.hash; } } } });