[Answered ]-How To Dynamically Create Elements in JavaScript

1👍

html:

<h2>
    test example two!
  test example two!
  test example two!
</h2>
<div class="container"></div>

css:

.holder {
 
  width:1200px;
  height:20px;
  display: flex;
}

js:

const subheading = document.querySelectorAll('h2');
const cont = document.querySelector('.container')
 subheading.forEach(function(x) {  
   cont.innerHTML = ''
   let xbe = x.innerText.split(' ');
   for (let i = 0; i<xbe.length;i++) {
   const test = document.createElement('div');
     test.classList.add('holder')
     let a = document.createElement('a');
     a.href=''
     a.innerText = xbe[i];
     console.log(a); 
     test.append(a);
     cont.append(test)
  }
   
    });

Leave a comment