Jquery how to remove the first x div?

i try to remove the first 4 divs if i click the 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 seems to remove them one by one:

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

thanks

+4
source share
2 answers

Use the selector :lt() .

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

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

NB "1" is not a valid class.

+11
source

Use :lt selector

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

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

+1
source

Source: https://habr.com/ru/post/1346798/


All Articles