Advertisement
didkoslawow

Untitled

May 25th, 2023
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function creeateElement(type, attributes, ...content) {
  2.   const res = document.createElement(type);
  3.  
  4.   for (let [attr, value] of Object.entries(attributes || {})) {
  5.     if (attr.substring(0, 2) == 'on') {
  6.       res.addEventListener(attr.substring(2).toLowerCase(), value);
  7.     } else {
  8.       res[attr] = value;
  9.     }
  10.   }
  11.  
  12.   content = content.reduce((a, c) => a.concat(Array.isArray(c) ? c : [c]), []);
  13.  
  14.   content.forEach(e => {
  15.     if (typeof e == 'string' || typeof e == 'number') {
  16.       const node = document.createTextNode(e);
  17.       res.appendChild(node);
  18.     } else {
  19.       res.appendChild(e);
  20.     }
  21.   });
  22.  
  23.   return res;
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement