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(); 

fiddle


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -