Patrioticcow
Patrioticcow

Reputation: 27048

jquery how to remove the first x div's?

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

Upvotes: 5

Views: 7437

Answers (2)

gen_Eric
gen_Eric

Reputation: 227270

Use the :lt selector

$('div.1:lt(4)', 'div.test').remove()

Example: http://jsfiddle.net/JD6CY/

Upvotes: 1

Matt Ball
Matt Ball

Reputation: 359836

Use the :lt() selector.

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

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

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

Upvotes: 12

Related Questions