GalinaKG

Function Create Js Dom Elements

Mar 28th, 2023
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function createElement(type, content, parentNode, id, classes, attributes){
  2.   const htmlElement = document.createElement(type)
  3.  
  4.   if(content && type !== 'input'){
  5.     htmlElement.textContent = content;
  6.   }
  7.  
  8.   if(content && type === 'input'){
  9.     htmlElement.value = content;
  10.   }
  11.  
  12.   if(id){
  13.     htmlElement.id = id;
  14.   }
  15.  
  16.   if (classes){
  17.     htmlElement.classList.add(...classes);
  18.   }
  19.  
  20.   if (parentNode){
  21.     parentNode.appendChild(htmlElement);
  22.     }
  23.  
  24.   //{ src: 'link to img', href: 'link to site' }
  25.   if (attributes){
  26.     for (const key in attributes) {
  27.       htmlElement.setAttribute(key, attributes[key]);
  28.     }
  29.   }
  30.   return htmlElement
  31. }
Add Comment
Please, Sign In to add comment