Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: Remove all elements after the first n elements?

I have something close to this:

<div class="batman-pictures">
    <a><img /></a>
    <a><img /></a>
    <a><img /></a>
    <a><img /></a>
    <a><img /></a>
    <a><img /></a>
</div>

I don't know how many <a><img /></a> that will be generated (I am fetching them from an API), and I want to delete all elements after the first three(n) <a><img /></a>. How can I do this in jQuery?

like image 781
allegutta Avatar asked May 12 '26 18:05

allegutta


1 Answers

Try this.

$(".batman-pictures a:nth-child(3)").nextAll().remove();
like image 112
Cláudio Barreira Avatar answered May 15 '26 07:05

Cláudio Barreira



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!