How to change color in JavaScript?

To change color in JavaScript, do this: element.style.color = 'red'.

For example, if this is your element with some text in it:

<span id="my-text">Hello World!</span>

Then you can change the color of Hello World! to, say, red with this JavaScript:

document.getElementById('my-text').style.color = 'red';

Background Color

Or you can change the background color of Hello World! to, say, yellow with this JavaScript:

document.getElementById('my-text').style.backgroundColor = 'yellow';