Last Updated: February 25, 2016
·
4.588K
· sir_ov

Create DOM elements with jQuery

Well this is the way I create DOM elements using jQuery or blocks of html code, for example to the append or prepend function, that's the most casual case I have seen and people write all the HTML in JS variables instead of write fewer lines and a beautiful code.

Example:

/* Creating an <img> element */
var $img = '<img src="lalala.xxx" width="350" height="250" />';

/* Instead of do that ^, try this */
var $img = $('<img>', { width : 350, height : 250, src : 'lalala.xxx' });

You can create any DOM element with this code, first param the tag, second parameter a JSON with the features.