Use map(), slice(), and wrapAll();
$(document).ready( function(){
var results =[];
var elements = $(".items").children('.boxgrid');
$.map( elements , function(i, n){
if( n%3 === 0 ){
results.push(n);
}
});
$.each( results , function(i,v){
elements.slice(v, v+3).wrapAll('<div class="row"></div>');
});
});
This is tested and works.