Last Updated: February 25, 2016
· 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.


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

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

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