jQuery Creating DOM elements


The jQuery function (usually aliased as $) can be used both to select elements and to create new elements.

var myLink = $('<a href="http://stackexchange.com"></a>');

You can optionally pass a second argument with element attributes:

var myLink = $('<a>', { 'href': 'http://stackexchange.com' });

'<a>' --> The first argument specifies the type of DOM element you want to create. In this example it's an anchor but could be anything on this list. See the specification for a reference of the a element.

{ 'href': 'http://stackexchange.com' } --> the second argument is a JavaScript Object containing attribute name/value pairs.

the 'name':'value' pairs will appear between the < > of the first argument, for example <a name:value> which for our example would be <a href="http://stackexchange.com"></a>