var $div = $(\'\').appendTo($(\'#header\'));
When creating new elements and adding them to the DOM, do you need the endi
Although it may work if you don't use a closing tag (or at least self close the tag), you should add a closing tag (self-close) (as mentioned, for cross-platform compatibility):
To ensure cross-platform compatibility, the snippet must be well-formed. Tags that can contain other elements should be paired with a closing tag:
$('');Alternatively, jQuery allows XML-like tag syntax (with or without a space before the slash):
$('');Tags that cannot contain elements may be quick-closed or not:
$(''); $('');
This is how jQuery creates the elements:
If the HTML is more complex than a single tag without attributes, as it is in the above example, the actual creation of the elements is handled by the browser's
innerHTMLmechanism. In most cases, jQuery creates a new element and sets theinnerHTMLproperty of the element to the HTML snippet that was passed in. When the parameter has a single tag, such as$('or')
$(''), jQuery creates the element using the native JavaScriptcreateElement()function.
Btw. you can also pass the data as second parameter:
$('', {'class': 'error', text: 'Error-Homie!'})