Advertisement
GalinaKG

Func Create DOM Elements

Mar 28th, 2023
586
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function createEl(type, attr, ...content) {
  2.     const element = document.createElement(type);
  3.     for (const item in attr) {
  4.         if (item == 'class') {
  5.             element.classList.add(attr[item]);
  6.         } else if (item == 'disable') {
  7.             element.disabled = attr[item];
  8.         } else {
  9.             element[item] = attr[item];
  10.         }
  11.     }
  12.  
  13.     for (let item of content) {
  14.         if (typeof item == 'string' || typeof item == 'number') {
  15.             item = document.createTextNode(item);
  16.         }
  17.         element.appendChild(item);
  18.     }
  19.     return element;
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement