Reputation: 13243
Here is the HTML:
<div class="line"></div>
<div class="line"></div>
<div class="line"></div>
<div class="line"></div>
Here is the Jquery:
if ( $( ".line" ).length > 2 ) {
//remove all other div.lines but keep the first 2
}
How do you do this?
Upvotes: 1
Views: 95
Reputation: 7887
$('.line:gt(1)').remove();
or
$('.line').each(function(pos) {
if(pos > 1) {
$(this).remove();
}
});
Upvotes: 1
Reputation: 193311
Combine with old good plain javascript:
$(".line").slice(2).remove();
Or you can use only jQuery:
$(".line").filter(':gt(1)').remove();
Upvotes: 1
Reputation: 15200
$(".line:gt(1)").remove();
You can use Jquery's gt selector. This select that indexes which are greater than 1.
See jsfiddle http://jsfiddle.net/X7a4Z/1/.
Upvotes: 3