Click here to Skip to main content
15,921,793 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello all,

i want to remove all child div tag except last one.. if parent has only one child then nothing to do.

suggest me selector

thanks in advance.
Posted

1 solution

Try this :-
HTML
<div id="pp">
       <div>
           1</div>
       <div>
           2</div>
       <div>
           3</div>
       <div>
           4</div>
       <div>
           5</div>
       <div>
           6</div>
   </div>
   <input type="button" id="removediv" value="remove" />

JavaScript
<script type="text/javascript">
        $('#removediv').click(function () {
            $("div#pp").contents().not('div:last-child').remove();
        });
</script>
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900