Skip to content

Instantly share code, notes, and snippets.

@magnusdahlstrand
Created February 8, 2012 15:10
Show Gist options
  • Save magnusdahlstrand/1770351 to your computer and use it in GitHub Desktop.
Save magnusdahlstrand/1770351 to your computer and use it in GitHub Desktop.
Ways of dom node creation and appending.
<a class="button icon clock run">run code</a>
var el = document.createElement('a');
el.setAttribute('class', 'button icon clock run');
el.innerHTML = 'run code';
document.body.appendChild(el);
$(body).append('<a class="button icon clock run">run code</a>');
var el = document.createElement('div');
el.innerHTML = '<a class="button icon clock run">run code</a>';
el = el.childNodes[0];
document.body.appendChild(el);
HTMLElement.__proto__.createEl = function(inEl) {
var el = document.createElement('div');
el.innerHTML = inEl;
el = el.childNodes[0];
this.appendChild(el);
return this;
}
HTMLElement.__proto__.createEl = function(inEl) {
var el = document.createElement('div');
el.innerHTML = inEl;
this.appendChild(el.childNodes[0]);
return this;
}
HTMLElement.__proto__.append = (function() {
var el = document.createElement('div');
return function(inEl) {
if(typeof inEl === 'string') {
el.innerHTML = inEl;
this.appendChild(el.childNodes[0]);
}
else if(inEl instanceof HTMLElement) {
this.appendChild(inEl);
}
return this;
}
}());
var $ = $ || {};
$.create = (function() {
var el = document.createElement('div');
return function(inEl) {
if(typeof inEl === 'string') {
el.innerHTML = inEl;
return el.childNodes[0];
}
else if(inEl instanceof HTMLElement) {
return inEl.cloneNode();
}
return this;
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment