javascript - Remove div between div using Jquery - Stack Overflow

I need to remove div elements which are loaded dynamically between two static div elements.HTML<div

I need to remove div elements which are loaded dynamically between two static div elements.

HTML

<div id="defaultFirst">
</div>
..
..
..
<div id="defaultLast">
</div>

There are some div elements which are loaded in between those two static div elements. Now I need to remove those before I append some other elements.

I tried using $('#defaultFirst').nextAll('div').remove() but that removed the #defaultLast element too.

I know that I can get the ids of the dynamic div and remove. But I need to know if there is any simpler way to remove those dynamic div elements?

I need to remove div elements which are loaded dynamically between two static div elements.

HTML

<div id="defaultFirst">
</div>
..
..
..
<div id="defaultLast">
</div>

There are some div elements which are loaded in between those two static div elements. Now I need to remove those before I append some other elements.

I tried using $('#defaultFirst').nextAll('div').remove() but that removed the #defaultLast element too.

I know that I can get the ids of the dynamic div and remove. But I need to know if there is any simpler way to remove those dynamic div elements?

Share Improve this question edited Apr 5, 2017 at 10:25 James Donnelly 129k35 gold badges214 silver badges223 bronze badges asked Apr 5, 2017 at 10:16 A CoderA Coder 3,0567 gold badges68 silver badges135 bronze badges 1
  • 1 try $('#defaultFirst').nextAll('div').not("#defaultLast").remove() or $('#defaultFirst').nextAll('div').not(".doNotRemoveClass").remove() for more elements – sTx Commented Apr 5, 2017 at 10:19
Add a ment  | 

2 Answers 2

Reset to default 11

Use nextUntil() instead of nextAll() and pass in a selector which selects your #defaultLast element:

$('#defaultFirst').nextUntil('#defaultLast').remove();

.nextUntil( [selector ] [, filter ] )

Description: Get all following siblings of each element up to but not including the element matched by the selector, DOM node, or jQuery object passed.

— jQuery's documentation on nextUntil()

If you have elements which aren't div elements between those two which you aren't wanting to remove, you can specifically remove only div elements by passing in a selector to your remove() call:

$('#defaultFirst').nextUntil('#defaultLast').remove('div');

I would remend getting to the cause of why the divs are appearing in the first place rather than hacking like this. However if you have to then the following should work

$('#defaultFirst').nextAll('div').not('#defaultLast').remove();

Codepen example - http://codepen.io/webknit/pen/ZeZXdQ

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743745279a4499891.html

相关推荐

  • javascript - Remove div between div using Jquery - Stack Overflow

    I need to remove div elements which are loaded dynamically between two static div elements.HTML<div

    6天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信