-1

I have a div where is an HTML Attribute called data-theme inside. I want to ask how do I change the value of the Attribute called data-theme. For example: From: data-theme="dark" to data-theme="light". The value is stored in a variable as a string.

My div:

<div id="myId" data-theme="dark"></div>

I want to use normal JavaScript.

Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44
Niklas
  • 436
  • 1
  • 4
  • 16

1 Answers1

0

document.getElementById("myId").setAttribute("data-theme", "newValue")

Ismail_Aj
  • 332
  • 1
  • 4
  • 17
  • 1
    Elements have a specialized `dataset` object which syncs with the corresponding data-attributes. `document.getElementById("myId").dataset.theme="light";`. – connexo Feb 08 '22 at 18:35