Search code examples
javascripthtmlshorthand

Shortest way to create a DIV


What would be the shortest way to do the following :

var div = document.createElement('div');
div.className = 'divClass';
div.innerHTML = 'Div Content';

... without any external libraries


Solution

  • class Div {
        constructor(className, innerHTML) {
            let div = document.createElement("div");
            div.className = className;
            div.innerHTML = innerHTML;
    
            return div;
        }
    }
    let innerHTML = "<a href=\"this.html\">LOL</a>"
    new Div(divClass, innerHTML);
    

    This would be the shortest way to doing it again and again while still having some order inside your code, IMO.