JSFiddle - React, Tailwind, and code Playground
HTML
<div id="container">
<div id="el1">Remove</div>
<div id="el2">Remove</div>
<div id="el3">Remove</div>
<div id="el4">Remove</div>
<script type="text/javascript">
// Keep
</script>
<div id="lastEl">Keep</div>
</div>
JavaScript
(function ($) {
var script = $('#container').children('#lastEl').prev();
// This returns a script. Everything before this needs to be removed
console.log(script)
}(jQuery));