Creativity is allowing yourself to make mistakes. Design is knowing which ones to keep.

Scott Adams

JQuery – Closing Tags in DOM Element Creation

December 28th, 2013

In most modern browsers this pattern:

$rootElement.append('<form>');

works fine and places a form element on the page inside the $rootElement. In IE8 (and I’m guessing earlier) however this pattern is required:

$rootElement.append('<form></form>');

The second pattern is probably the correct way to form a new element in a jQuery .append() but if you are used to getting away with the former then it may bamboozle you a bit when it fails inIE.

Neither of these is ideal however as this pattern:

$rootElement.append(document.createElement('form'))

is faster (DOM object creation on jsperf).

Categories:
Web Design|IE Gotchas