Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can a new HTML element with a specific class name be created with JavaScript?

I am trying to create an HTML element with a specific class name so when the content is on the DOM the CSS styles can be applied to it. It this possible ?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Just create the element and insert class inside it.

Ex:

// inside createElement function you have to type the tag name
const element = document.createElement('div')
element.classList.add('class-name')
// then you need to insert the element inside any element you want ( for example body element )
document.body.appendChild(element)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading