Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery how to remove the first x div's?

Tags:

jquery

i an trying to remove the first 4 divs if i click a button:

<div class="test">
<div class="1"></div>
<div class="1"></div>
<div class="1"></div>
<div class="1"></div>
<div class="1"></div>
<div class="1"></div>
</div>

i;ve tried this, but it seems to remove them one by one:

if ($('.test').find('.1').size() >= 4) {
$('.test').find('.1').remove();
}

thanks

like image 456
Patrioticcow Avatar asked Apr 05 '11 20:04

Patrioticcow


1 Answers

Use the :lt() selector.

$('.test').find('.1:lt(4)').remove();

Demo: http://jsfiddle.net/mattball/kR3wL/

N.B. "1" is not a valid class.

like image 124
Matt Ball Avatar answered Sep 23 '22 14:09

Matt Ball