Search code examples
javascriptjqueryhtmlexecute

Keep only the first three divs


I own a div and within it thousands of others, but I like to keep only the first 3, how to make it work with jquery? Exemple:

<div class="owner">
  <div class="child"></div>
  <div class="child"></div>
  <div class="child"></div> 
  <div class="child"></div> -> DELETE
  <div class="child"></div> -> DELETE
  <div class="child"></div> -> DELETE
  <div class="child"></div> -> DELETE 
  [...]
</div>

Solution

  • All those jquery solutions do work. But if you also want to do it the CSS way, you can simply do:

    .owner .child:nth-child(1n+4) { display: none; }