Skip to content

Instantly share code, notes, and snippets.

@jtenner
Last active August 29, 2015 14:00
Show Gist options
  • Save jtenner/ec09ffecb034a8a05cb7 to your computer and use it in GitHub Desktop.
Save jtenner/ec09ffecb034a8a05cb7 to your computer and use it in GitHub Desktop.
;(function(d){
'use strict';
function init () {
// create h1 element and set some attributes to it
var h1 = FSP('h1', {
'id': 'main-h1',
'class': 'class_1 class_2',
'data-name': 'created h1'
});
// create a element and set some attributes to it
var a = FSP('a', {
'href': './contact.html',
'id': 'my-a',
'class': 'link_1',
'target': '_blank'
});
// add some text
h1.innerHTML = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Numquam, nam?';
a.innerHTML = 'Link to contact.html';
// append to div whose id is c
d.getElementById('c').appendChild(h1);
d.getElementById('c').appendChild(a);
}
init();
})(document);
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Create Elements</title>
</head>
<body>
<div id="c"></div>
<script src="library.js"></script>
<script src="how-to-use.js"></script>
</body>
</html>
;(function(w, d, undefined){
'use strict';
var Module = (function(){
var exports = {};
exports.FSP = function FSP(el, attributes) {
var element = d.createElement(el);
for (var key in attributes) {
if (attributes.hasOwnProperty(key)) {
element.setAttribute(key, attributes[key]);
}
}
return element;
};
// expose to public
return exports;
})();
// add to window object
w.FSP = Module.FSP;
})(window, document);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment