Is $("<div />").appendTo($mySelector)
safe to use? Will it work in every browser even though is not a valid HTML element?
I'm asking because it's really easier to use than $("<div><div />")
Is $("<div />").appendTo($mySelector)
safe to use? Will it work in every browser even though is not a valid HTML element?
I'm asking because it's really easier to use than $("<div><div />")
Yes. If you look at the source this case is even optimized.
/^<(\w+)\s*\/?>(?:<\/\1>|)$/
<div / > // your case
This regexp will match if there is or is not a /
, and it is not included in a group. The only grouped expression is the actual tag name (div
), which is used.
If you look more closesly you can see that the following are all handled the very same way:
<div>
<div />
<div></div>
<div /></div>
Yes, but why not use $(document.createElement('div'));
it is faster than using the jquery to create the element. It will work without running a regex and requiring translation by jquery.
See this SO answer for a test of using different methods for element creation