Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove 3 last divs with jQuery

<div id="widgetAreaFooter">
<div class="row">1</div>
<div class="row">2</div>
<div class="row">3</div>
<div class="row">4</div>
<div class="row">5</div>
<div class="row">6</div>
<div class="row">7</div>
</div>

How to remove the 3 last div ?

I tryed this but it doesn't work :/

var row = $( '#widgetAreaFooter>.row' );
var nbr = row.length ;

for ( var i=4;i<nbr;i++ ) row.get(i).remove();
or
for ( var i=4;i<nbr;i++ ) row[i].remove();
like image 249
zep Avatar asked Jul 08 '11 10:07

zep


1 Answers

This will remove the last three elements:

$('#widgetAreaFooter > .row').slice(-3).remove();

jsFiddle Demo

  • You can get a part of a jQuery collection using .slice().

    If a negative number is provided, this indicates a position starting from the end of the set, rather than the beginning.

like image 143
kapa Avatar answered Oct 09 '22 07:10

kapa