Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

selecting nth div (and beyond) in jQuery

Say I have a div containing an unlimited number of child divs. Is there an easy way to get jQuery to select the nth div and every div after it so I can change those (in this case, call remove() on old divs)?

like image 589
ceejayoz Avatar asked Dec 05 '25 06:12

ceejayoz


2 Answers

You can use the ":gt()" selector:

 // div's 10 and higher
 $('div:gt(9)').show()
like image 74
Rob Avatar answered Dec 07 '25 21:12

Rob


Typing this out of my head and the jQuery API doc (read: this is not tested), but the first thing I'd do is to

$('#container div').slice(-n).remove();
like image 32
pilif Avatar answered Dec 07 '25 19:12

pilif



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!