Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a class to a DOM element in JavaScript?

Tags:

javascript

dom

How do I add a class for the div?

var new_row = document.createElement('div'); 
like image 367
wow Avatar asked Jul 12 '09 04:07

wow


People also ask

Can you add class to an object Javascript?

To add a class to an element, without removing/affecting existing values, append a space and the new classname, like so: document. getElementById("MyElement").


1 Answers

This answer was written/accepted a long time ago. Since then better, more comprehensive answers with examples have been submitted. You can find them by scrolling down. Below is the original accepted answer preserved for posterity.


new_row.className = "aClassName"; 

Here's more information on MDN: className

like image 144
Darko Z Avatar answered Oct 02 '22 15:10

Darko Z