I was wondering if it were possible to, instead of selecting an element from the DOM via a jQuery selector, if I can create one instead.
For example:
$.create('#hello').insertAfter('#test');
would generate the following:
<div id="test">This element already existed in the DOM.</div>
<div id="hello"></div>
Or, better yet, for more complex operations:
$.create('#test.a.b');
for:
<div id="test" class="a b"></div>
Obviously, more complex operations like :selected or :nth-child or :not() and all of those need not be implemented.
Is anyone aware of a solution?
Thanks.
To create
<div id="hello"></div>try
To create
<div id="test" class="a b"></div>same as$('<div id="test" class="a b"></div>')will create new element but not add it to DOM.To add that element you need to use append() or appendTo() or insetAfter() or insertBefore() and so on.
It is better to use a function to:
use the function like:
You can also append newly created element to any other target instead of
body.I think following function will help you:
DEMO