0 points

Javascript create dynamic elements function


dashboard

Wed Jul 01 2020 12:12:44 GMT+0000 (UTC)

Posted by @peota #javascript

function createElement(type, attributes) {
  var element = document.createElement(type);
  for (var key in attributes) {
    if (key == "class") {
        element.classList.add.apply(element.classList, attributes[key]); // add all classes at once
    } else {
      element[key] = attributes[key];
    }
  }
  someElement.appendChild(element);
}
content_copy Copy

https://stackoverflow.com/questions/43168284/javascript-createelement-function