Use the document.createElement() method to create the element.

 

Use  document.createElement() method to create the element and setAttribute() to set ID of an element 

following is  the examples in this article.

const el = document.createElement('div');


el.setAttribute('id', 'newId');


el.textContent = 'Hello world';

el.innerHTML = '<span>Hello world</span>';


const box = document.getElementById('someElementId');

box.appendChild(el);

Tags:

Share:

Related posts