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)?
views:
682answers:
3
+2
A:
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();
pilif
2009-01-19 19:51:33
Worked, but I'm going to go with the :gt() selector @Rob posted. Thanks though, have an upvote!
ceejayoz
2009-01-19 20:03:50
yepp. the :gt() solution is much better because it does not retrieve elements just to throw them away afterwards. This is why I have upvoted Rob's answer too. Next time, I'll refrain from posting answers I don't *really* know.
pilif
2009-01-19 20:15:57
+13
A:
You can use the ":gt()" selector:
// div's 10 and higher
$('div:gt(9)').show()
Rob
2009-01-19 19:53:08
+2
A:
Or if you need to do something with all divs first:
$('div').css('color', 'red').filter(':gt(5)').remove();
Pim Jager
2009-01-19 19:55:26