Home > Software engineering >  How to set css property that are inside a tag
How to set css property that are inside a tag

Time:05-04

I can set all property in my entire html using:

document.getElementsByTagName('html')[0].style.setProperty(--color-dustomized, "red");

But I just wanted to change the tags that are INSIDE the div with id class2, I tried something like this, but the error appears Property 'style' does not exist on type 'Element'

document.getElementsByTagName('html')[0].getElementsByClassName('class2')[0].style.setProperty(--color-dustomized, "red"); // Property 'style' does not exist on type 'Element'.

I can't use it this way as I don't know how many div will be on the page:

document.getElementsByTagName('html')[0].getElementsByTagName('div')[1]

document.getElementsByTagName('html')[0].getElementsByClassName('class2')[0].style.setProperty(--color-dustomized, "red");
<div id="class1" >
  <!-- code -->
</div>

<div id="class2" >
  <!-- code -->
</div>

CodePudding user response:

Have you tried this?

`document.getElementById("class1").style.color="red";`

CodePudding user response:

Try this

document.getElementById("class1").style["--color-dustomized"] = "red"

CodePudding user response:

just type this:

document.getElementById("class1").style.setProperty("--color-dustomized","red");

  • Related