jQuery - Remove all child elements before specific child -
jsfiddle: https://jsfiddle.net/9krvkfpw/
hi,
i'm trying remove child elements point i'm not sure effective way is.
here's example code:
<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>
essentially, want remove elements above script element. can't find first script element because there's more above 1 want keep, they'll in way.
my idea script tag hitting #lastel
, getting previous element.
like this:
var script = $('#container').children('#lastel').prev();
from here i'm not sure best way proceed is, or if best way?
i script.prev().remove()
multiple times, isn't going work because there's different number of elements previous script i'm starting with.
any ideas great!
thanks!
to answer looking exactly:
essentially, want remove elements above script element
you can following:
$('#container script').prevall().remove();
Comments
Post a Comment