-2

How would I change the class attribute in my HTML to "night nosmoke" using JavaScript?

<div id="imperium" class="day nosmoke" style="margin-left: -800px; margin-top: -600px;">

I've tried copying the jspath then look at innerHTML but that just brings other stuff up.

Dour High Arch
  • 21,513
  • 29
  • 75
  • 90
Krizzo
  • 25
  • 4
  • 2
    *change day into night here with javascript* o_O – Liam Jun 18 '20 at 14:14
  • 1
    If you look closer, this is actually a genuine question, based on the experience of the user with js/html ... Just change the class with Javascript like so: `document.getElementById("imperium").className = "night nosmoke";` Here's more to view: https://stackoverflow.com/questions/195951/how-can-i-change-an-elements-class-with-javascript – Cypherjac Jun 18 '20 at 14:21

1 Answers1

4

I guess this is what you mean. Remove day class and add night class to the element:

document.getElementById('imperium').classList.remove('day');// <---- Remove day
document.getElementById('imperium').classList.add('night'); //<---add class night
console.log(document.getElementById('imperium'))
<div id="imperium" class="day nosmoke" style="margin-left: -800px; margin-top: -600px;">
ABGR
  • 4,631
  • 4
  • 27
  • 49