Change background color using javascript function

Example

Set a background color for a document:

document.body.style.backgroundColor = "red";

Try it Yourself »

More "Try it Yourself" examples below.

Definition and Usage

The backgroundColor property sets or returns the background color of an element.

Browser Support

The numbers in the table specify the first browser version that fully supports the property.

Property
backgroundColor 1.0 4.0 1.0 1.0 3.5

Syntax

Return the backgroundColor property:

object.style.backgroundColor

Set the backgroundColor property:

object.style.backgroundColor = "color|transparent|initial|inherit"

Property Values

ValueDescription
color Specifies the background color. Look at CSS Color Values for a complete list of possible color values
transparent Default. The background color is transparent [underlying content will shine through]
initial Sets this property to its default value. Read about initial
inherit Inherits this property from its parent element. Read about inherit

Technical Details

Default Value:Return Value:CSS Version
transparent
A String, representing the background color
CSS1

More Examples

Example

Set a background color of a specific

element:

document.getElementById["myDiv"].style.backgroundColor = "lightblue";

Try it Yourself »

Example

Return the background color of a specific

element:

alert[document.getElementById["myDiv"].style.backgroundColor];

Try it Yourself »

Example

Return the background color of a document:

alert[document.body.style.backgroundColor];

Try it Yourself »

Related Pages

CSS tutorial: CSS Background

CSS reference: background-color property

HTML DOM reference: background property



  1. HowTo
  2. JavaScript Howtos
  3. Change the Background Color in JavaScript

Created: July-24, 2021

This tutorial will discuss how to change the background color using the backgroundColor property in JavaScript.

Change the Background Color Using the backgroundColor Property in JavaScript

We can change the background color using the backgroundColor property in JavaScript. To use this property, you need to get the element whose background color you want to change, and then you can use the backgroundColor property to set the background color.

For example, let’s create a page using HTML and change the background color of the body to green using the backgroundColor property. See the code below.




    


    
        document.body.style.backgroundColor = 'green';
    


You can also get the element with the id or name of the class. To get an element using its id, you can use the getElementById[] function. To get an element using its class, you can use the getElementsByClassName[] function.

For example, let’s get an element by its id and change its background color using the backgroundColor property. See the code below.




    


    

Hello Wold

document.getElementById['myID'].style.backgroundColor = 'green';

The code above will only change the background color of the element with id myID and not the whole body section. You also change the background color of an element using the color code of different colors instead of changing it using the color name.

Let’s write a code to change the background color of a text using a button. See the code below.




    


Change Color
Hello World
function Mycolor[] { var element = document.getElementById["myID"]; element.style.backgroundColor='#900'; }

In this code, you will see a button and a text. When you press the button, the background color of the text will change according to the given color.

Related Article - JavaScript Color

  • JavaScript Random Color Generator
  • RGB Color Model in JavaScript
  • Convert RGB to HEX in JavaScript

    Related Article - JavaScript Background

  • JavaScript Random Color Generator
  • RGB Color Model in JavaScript
  • Convert RGB to HEX in JavaScript
  • How do you change the background color in JavaScript?

    We can change the background color using the backgroundColor property in JavaScript. To use this property, you need to get the element whose background color you want to change, and then you can use the backgroundColor property to set the background color.

    How do I change the color of something in JavaScript?

    To change the text color of a given element, first we need to access it inside the JavaScript by using the document. getElementId[] or document. querySelector[] methods and set its style. color property to your desired color.

    How do I change the background color in Click JS?

    Change the element's background color on click #.
    Add a click event listener to the element..
    Assign the event object to a variable in the function..
    Set the event. target. style. backgroundColor property to the specific background color..

    Which property is used to change the background color in JavaScript?

    The background-color CSS property sets the background color of an element.

    Chủ Đề