jQuery.create() - jQuery plugin to create HTML elements
jQuery.create() - it's a featured plugin for jQuery JavaScript Library.
You can create any types of HTML elements, set attributes and context or HTML using jQuery.create().
See below the HTML example declaration.
I think it make easy and fast your programming in JavaScript.
jQuery.create = function() {
if (arguments.length == 0) return [];
var args = arguments[0] || {}, elem = null, elements = null;
var siblings = null;
// In case someone passes in a null object,
// assume that they want an empty string.
if (args == null) args = "";
if (args.constructor == String) {
if (arguments.length > 1) {
var attributes = arguments[1];
if (attributes.constructor == String) {
elem = document.createTextNode(args);
elements = [];
elements.push(elem);
siblings =
jQuery.create.apply(null, Array.prototype.slice.call(arguments, 1));
elements = elements.concat(siblings);
return elements;
} else {
elem = document.createElement(args);
// Set element attributes.
var attributes = arguments[1];
for (var attr in attributes)
jQuery(elem).attr(attr, attributes[attr]);
// Add children of this element.
var children = arguments[2];
children = jQuery.create.apply(null, children);
jQuery(elem).append(children);
// If there are more siblings, render those too.
if (arguments.length > 3) {
siblings =
jQuery.create.apply(null, Array.prototype.slice.call(arguments, 3));
return [elem].concat(siblings);
}
return elem;
}
} else return document.createTextNode(args);
} else {
elements = [];
elements.push(args);
siblings =
jQuery.create.apply(null, (Array.prototype.slice.call(arguments, 1)));
elements = elements.concat(siblings);
return elements;
}
};
HTML example declaration.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Page</title>
<script type="text/javascript"
src="../App_Scripts/jQuery/jQuery-1.2.3.js"></script>
<!-- Utils.js - contain jQuery.create - plug-in -->
<script type="text/javascript"
src="../App_Scripts/Utils/Utils.js"></script>
<script type="text/javascript">
$(document).ready(function() {
// Create new element typeof DIV
var o = $.create('div',
{'id':'wrapper-header',
'class':'header'}, ['Hello world!!!']);
// Append object o
$('#wrapper').append($(o));
});
</script>
</head>
<body>
<div id="wrapper"></div>
</body>
</html>