selecting nth div (and beyond) in jQuery
-
19-08-2019 - |
Question
Say I have a div
containing an unlimited number of child div
s. 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 div
s)?
Solution
You can use the ":gt()" selector:
// div's 10 and higher
$('div:gt(9)').show()
OTHER TIPS
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();
Or if you need to do something with all divs first:
$('div').css('color', 'red').filter(':gt(5)').remove();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow