[Home] [Blog] [Contact] - [Talks] [Bio] [Customers]
twitter linkedin youtube github rss

Patrick Debois

Jquery Create: Clean way of creating DOM Elements instead of string concatenation


(copyright evalinux)
Thanks to Basil Goldman, we can now write cleaner Jquery code. When creating elements for the DOM structure, we often resort to string concatenation because the wrap() and other function look kind of cumbersome in our IDE.

It feels also more natural when adding these items to your DOM.

http://blogs.microsoft.co.il/blogs/basil/archive/2008/08/21/jquery-create-jquery-plug-in-to-create-elements.aspx

Example:
var element_close=$.create('input',{'class':'element_close','type':'button','value':'Close'});

var element_wrapper=$.create('div',{'class':'element_wrapper'});

$(element_wrapper).append($(element_close));


Note: Be aware of what this is doing. It will create elements and insert them into the DOM. Especially in loops this can be slower then doing String HTML concatenation. (See https://www.jedi.be/blog/2008/10/10/is-your-jquery-or-javascript-getting-slow-or-bad-performance/ )