Skip to content

Instantly share code, notes, and snippets.

@berlysia
Last active January 3, 2016 02:29
Show Gist options
  • Save berlysia/8395594 to your computer and use it in GitHub Desktop.
Save berlysia/8395594 to your computer and use it in GitHub Desktop.
element creator with emmet-like string
function(selector){
var arr = selector.replace(/([.#])/g,",$&").split(',');
var classes = arr.filter(function(e){return e.substr(0,1) == '.'}).map(function(e){return e.slice(1)});
var id = arr.filter(function(e){return e.substr(0,1) == '#'})[0];
var elementName = 'div';
if(!(arr[0] == '' || arr[0].substr(0,1).match(/[.#]/))) elementName = arr[0];
var newElement = document.createElement(elementName);
if(classes.length>0) newElement.className = classes.join(' ');
if(id) newElement.id = id.slice(1);
return newElement;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment