1

I have a parent div called examples. I want to remove the last div in that parent div. How can I do so using jquery?

<div class="examples">
  <div class="test1"></div>
  <div class="test2"></div>
</div>

I want to remove the last child of examples, which in this case is test2 div

Pota Onasys
  • 1,302
  • 3
  • 15
  • 18
  • 1
    Possible duplicate of [remove last append element jquery](http://stackoverflow.com/questions/1650463/remove-last-append-element-jquery) – Climbatize Dec 03 '15 at 19:14

6 Answers6

4

try this one:

$('div.examples').children().last().remove();

Rizwan Khan
  • 463
  • 2
  • 6
  • 20
2

You can use find to traverse the child elements. Use div:last to select the last one, and .remove to remove it.

$(".examples").find("div:last").remove();
Andrew Brooke
  • 11,873
  • 8
  • 36
  • 55
1

Try this

$('.examples').children().last().remove()
Nikhil Aggarwal
  • 27,657
  • 4
  • 40
  • 56
1

Just use :last filter.

$('.examples div:last').remove();

http://jsfiddle.net/2a94hc90/

DinoMyte
  • 8,615
  • 1
  • 17
  • 26
0

If you want the last child of parent div is removed on clicking inner divs, try this one

$(this).parent().children().last().remove();

0

Try using :nth-last-of-type() selector with parameter 1

$(".examples div:nth-last-of-type(1)").remove()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<div class="examples">
  <div class="test1">test1</div>
  <div class="test2">test2</div>
</div>
guest271314
  • 1
  • 12
  • 91
  • 170