jquery - "Prepare" Elements to be removed -


suppose have list following:

<ul id="list">     <li>1</li>     <li>2</li>     <li>3</li>     <li>4</li> </ul> 

and want use jquery remove li elements that's simple enough:

$('#list li').each(function() {     if(some condition true)         $(this).remove(); }); 

this causes multiple manipulations dom. in order improve perfomance have manipulate dom once.

therefore have somehow "mark" or remember of li's remove , call jquerys "remove()" method once of these li's.

whats best way this?

see jsfiddle here: http://jsfiddle.net/rfrhm/

you can clone list , manipulate in memory (i think jquery uses fragments this), replace entire list manipulated one:

var $list = $('#list').clone(true); $list.children().each(function() {     if ( condition ) {         $(this).remove();     } }); $('#list').replacewith($list); // 1 dom manip 

i’m not sure increases performance, require 1 dom manipulation if that’s you’re after.

demo: http://jsfiddle.net/3y5nl/


Comments