Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery remove all elements until id='whatever' found

Need to remove all the code from the start tag to the next

I've tried this

$('#page1').remove();

But that only removes what's between the tag.

And I don't know what else could be between the page1 and page2 tags as the code is dynamically added based on the types of form elements on the page

<div id='page1' name='page1'>
 ...
</div> 
<div id='another element' />
<div id=yet 'another element' />
...
<!-- Need to remove from page1 to here -->
<div id='page2' name='page2'>
 ...
</div>
like image 871
Phill Pafford Avatar asked Dec 04 '22 22:12

Phill Pafford


1 Answers

Assuming you want to remove page1 and everything until page2 you can do this:

$("#page1").nextUntil("#page2").andSelf().remove();

Example on jsfiddle

like image 78
Mark Coleman Avatar answered Dec 08 '22 14:12

Mark Coleman