rgvcorley rgvcorley - 6 months ago 10
jQuery Question

Best way to create new DOM elements

Using jquery is it better to create a DOM element like this:-

function create(options)
{
$('<form action="' + options.action + '"></form>');
}


Or like this:

function create(options)
{
$form = $('<form></form>');
$form.attr('action',options.action);
}


This may be a matter of opinion. I feel that the second way gives more clarity but I suspect it is less efficient...

Answer

jQuery can create object as following code

$form = $('<form>', {action: 'my action'});

class is a reserved word in IE and needs to be quoted. See full list of reserved words: Reserved Keywords in Javascript