Alex Rivera | Logout

What is the most efficient way to create HTML elements using jQuery?

Asked 2008-11-29T02:15:53.603
434

Recently I've been doing a lot of modal window pop-ups and what not, for which I used jQuery. The method that I used to create the new elements on the page has overwhelmingly been along the lines of:

$("<div></div>");

However, I'm getting the feeling that this isn't the best or the most efficient method of doing this. What is the best way to create elements in jQuery from a performance perspective?

This answer has the benchmarks to the suggestions below.

Edit
Report

3 Answers

21

This is not the correct answer for the question but still I would like to share this...

Using just document.createElement('div') and skipping JQuery will improve the performance a lot when you want to make lot of elements on the fly and append to DOM.

answered 2010-02-05T13:39:06.750
16

I think you're using the best method, though you could optimize it to:

 $("<div/>");
answered 2008-11-29T02:22:14.697
7

One point is that it may be easier to do:

$("<div class=foo id=bar style='color:white;bgcolor:blue;font-size:12pt'></div>")

Then to do all of that with jquery calls.

answered 2010-09-23T20:13:46.773

Your Answer