1

Is it possible to add multiple elements to the DOM, and have the browser do a single reflow/repaint after they're all added? I'm following this guy's advice, but the elements need to be added at various places throughout the page.

For example,

<div>
 <div id="A">
 <p>stuff...</p>
 </div>
 <div id="B">
 <p>stuff...</p>
 </div>
 ...
</div>

I'd like to be able to do something like this:

$("#A").append('<img src="a.png"/>);
$("#B").append('<img src="b.png"/>);
...

.. and have the browser wait to reflow/repaint after the last image is added.

asked Jun 6, 2013 at 20:52
0

1 Answer 1

2

Pull a clone of the parent element into memory, alter it, and write it all back out at once using .replaceWith():

$p = $('#A').parent(); // or select it any way you like
$pc = $p.clone();
$pc.find('#A').append('<img src="a.png"/>');
$pc.find('#B').append('<img src="b.png"/>');
$p.replaceWith($pc);

http://jsfiddle.net/mblase75/fSnLb/

answered Jun 6, 2013 at 20:59

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.