Although the usual way to do it is a two-step technique, try this:

const el = Object.assign(document.createElement('div'), { className: 'foo' });

console.log(el);
console.log(el.className);
Run code snippetEdit code snippet Hide Results Copy to answer Expand

Answer from Majed Badawi on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › Document › createElement
Document: createElement() method - Web APIs | MDN
If we wanted to create an instance of this element programmatically, we'd use a call along the following lines: ... The new element will be given an is attribute whose value is the custom element's tag name.
Discussions

How to append multiple classes to an element? JavaScript DOM
Use classList instead of setAttribute. setAttribute should be avoided for setting classes, as it will overwrite whatever is already set, making adding classes overly complicated. Also, avoid making multiple appends to your DOM whenever possible. Build up the section element, appending all of its child elements, then append the section element to your "workspace." Also also, the code you have written does not match the HTML. You are appending the city div outside the section, instead of nested in an H2 element. More on reddit.com
🌐 r/learnjavascript
4
2
October 10, 2020
How can I add a class to a DOM element in JavaScript? - Stack Overflow
You need to split the className ... to your element. The classList way addresses this problem and can be used even if the class was set the className way. It is easy to manipulate classes when you need to. You can add, remove or check for them as you wish! It can be used with single or multiple classes. Copy// Create a div and ... More on stackoverflow.com
🌐 stackoverflow.com
Setting class to a DOM element
That's not a syntax error in itself, but it's such an unusual thing that it's often a sign of something else wrong. The "left side" assignment replaces the element you just created in task 1 with a string, which explains the error message. And the "right side" assignment is setting the class of the ... More on teamtreehouse.com
🌐 teamtreehouse.com
1
April 28, 2017
Problem with document.getElementsByClassName().innerHTML
document.getElementsByClassName() return an array so you need to change your code to document.getElementsByClassName('insert-your-classname-here')[0].innerHTML = 'HI'; More on reddit.com
🌐 r/javascript
10
0
March 25, 2018
🌐
DEV Community
dev.to › amersikira › 4-easy-ways-to-add-class-to-an-element-using-javascript-46p1
4 easy ways to add class to an element using JavaScript - DEV Community
November 13, 2021 - We will explore 4 ways on how to add class to any element in the DOM, including body and html element. The most straightforward way of adding a class is using className propery. It is a “native” way of adding class to an element.className ...
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-create-element-with-class
Create an Element with Classes or ID in JavaScript | bobbyhadz
The el.className = 'bg-yellow text-lg'; line directly sets the bg-yellow and text-lg classes on the element by updating its className property.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › Element › className
Element: className property - Web APIs | MDN
3 weeks ago - The className property of the Element interface reflects the element's class content attribute.
Find elsewhere
🌐
Quora
quora.com › Can-you-create-an-element-in-JS-with-an-ID-or-a-class
Can you create an element in JS with an ID or a class? - Quora
Answer (1 of 4): There are times you might want to dynamically generate elements on a page and add classes to style them. You can create elements with ids and/or classes with plain Javascript with the following commands: [code]var newElement = document.createElement(“div”); // to add a ...
🌐
HTML Dog
htmldog.com › guides › javascript › advanced › creatingelements
Creating Elements | HTML Dog
Here’s an example that creates ... y'all?"; div.setAttribute('class', 'note'); document.body.appendChild(div); So document.createElement is used with an HTML tag to create the element....
🌐
Reddit
reddit.com › r/learnjavascript › how to append multiple classes to an element? javascript dom
r/learnjavascript on Reddit: How to append multiple classes to an element? JavaScript DOM
October 10, 2020 -

I'm sooo sorry if this is a massively newbie question, but I'm not quite sure how to search for this. Everything I find shows me how to append 1 class to an element. Essentially what I'm trying to do is convert this HTML to Javascript DOM:

        <section class="location">
          <h2>
            <div class="city">Pompano Beach, US</div>        
          </h2>
          <div class="date">Fri, August 4, 2020</div>
        </section>

This is what I have so far:

	var loc = document.createElement("SECTION");
	loc.setAttribute("class", "location");
	workspace.appendChild(loc);

	var city = document.createElement("DIV");
	city.setAttribute("class", "city");
	workspace.appendChild(city);

	let h3 = document.createElement('h3');
	text = document.createTextNode("Pompano Beach, US");
	h3.appendChild(text);
	workspace.getElementsByClassName("location").appendChild(h3);

Anything would be helpful!! Thank you!

🌐
OOZOU
oozou.com › home › til › how to create an html element with attributes in one line in javascript
How to Create an HTML Element with Attributes in One Line in JavaScript | TIL | OOZOU
October 13, 2020 - ... Object.assign(document.createElement('a'), { id: 'id', class: 'whatever class', href: 'https://www.google.com/', innerHTML: 'This is a link' }); With this one-liner, you're creating an anchor (<a>) tag and setting its id, class, href, and ...
🌐
Nikita Hlopov
nikitahl.com › create-element-with-javascript
How to create a new element with class name in JavaScript
const newAnchor = Object.assign( document.createElement('a'), { innerText: 'Apply Now', href: 'https://example.com', className: 'link', target: '_blank', rel: 'noopener noreferrer' } ) document.body.appendChild(newAnchor) ... ⟨ Previous A list of basic Git commands for beginners for everyday use Next ⟩ A complete list of methods to add an element to DOM with JavaScript
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › Web_components › Using_custom_elements
Using custom elements - Web APIs | MDN
September 1, 2026 - The <popup-info> custom element takes an image icon and a text string as attributes, and embeds the icon into the page. When the icon is focused, it displays the text in a pop up information box to provide further in-context information. ... To begin with, the JavaScript file defines a class called PopupInfo, which extends the HTMLElement class. ... // Create a class for the element class PopupInfo extends HTMLElement { constructor() { // Always call super first in constructor super(); } connectedCallback() { // Create a shadow root const shadow = this.attachShadow({ mode: "open" }); // Create
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript dom › javascript createelement() method
JavaScript document.createElement() By Practical Examples
January 18, 2020 - If you want to add an id to a div, you set the id attribute of the element to a value, like this: let div = document.createElement('div'); div.id = 'content'; div.innerHTML = '<p>CreateElement example</p>'; document.body.appendChild(div);Copy · The following example sets the CSS class of a new div note:
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › Element › classList
Element: classList property - Web APIs | MDN
const div = document.createElement("div"); div.classList = "foo"; // forwarded to classList.value // our starting state: <div class="foo"></div> console.log(div.outerHTML); // use the classList API to remove and add classes div.classList.remove("foo"); div.classList.add("another-class"); // <div class="another-class"></div> console.log(div.outerHTML); // if visible is set remove it, otherwise add it div.classList.toggle("visible"); // add/remove visible, depending on test conditional, i less than 10 div.classList.toggle("visible", i < 10); // false console.log(div.classList.contains("foo")); /
🌐
W3Schools
w3schools.com › howto › howto_js_add_class.asp
How To Add a Class Name
Add a class name to the div element with id="myDIV" (in this example we use a button to add the class). <button onclick="myFunction()">Try it</button> <div id="myDIV"> This is a DIV element.
🌐
Team Treehouse
teamtreehouse.com › community › setting-class-to-a-dom-element
Setting class to a DOM element (Example) | Treehouse Community
April 28, 2017 - Hello, when I type this code: const contentDiv = document.getElementById("content"); let newParagraph = document.createElement('p'); newParagraph = document.className = "panel"; I get an error that the Task 1 is no longer passing.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-adding-a-class-name-to-the-element
JavaScript adding a class name to the element - GeeksforGeeks
July 11, 2025 - It adds a class to the element without affecting any other classes the element may already have. ... Example: This example uses .add() method to add the class name. ... <!DOCTYPE html> <html> <head> <title> JavaScript Adding a class name to the element </title> <style> .addCSS { background-color: green; color: white; padding: 20px; font-size: 25px; } </style> </head> <body style="text-align:center;"> <h1 style="color:green;"> GeeksforGeeks </h1> <p id="p"> A Computer Science portal for Geeks.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-add-a-class-to-dom-element-in-javascript
How to Add a Class to DOM Element in JavaScript? - GeeksforGeeks
August 5, 2025 - Adding a class to a DOM (Document Object Model) element in JavaScript is a fundamental task that enables developers to dynamically manipulate the appearance and behavior of web pages.
🌐
TypeScript
typescriptlang.org › docs › handbook › 2 › classes.html
TypeScript: Documentation - Classes
As with other JavaScript language features, TypeScript adds type annotations and other syntax to allow you to express relationships between classes and other types. ... This class isn’t very useful yet, so let’s start adding some members. A field declaration creates a public writeable property on a class: