How to Change Background Color in JavaScript?19 Apr 2025 | 4 min read What is the background color?Using the background color feature, it is possible to insert a background color for a web page. The background-color property within an HTML element will be responsible for this. This affects the whole size of the element that has been specified, except for the margin. The default background color property is set to be transparent. Background colors in HTML are defined using either named values, HEX color codes, RGB color values, or HSL values. Changing background colorBasic but crucial, it alters the background color of a webpage or an HTML element and represents the manipulation of the DOM. JavaScript allows various methods for such as adjusting CSS properties directly, using event listeners, and reacting to user events. When developing the content for a web page, it should be embedded within the <body> tag. The color of the background of a web page can be changed with the help of bgcolor available in JavaScript. Several attributes are provided by JavaScript that impact the properties of the content, thereby dictating the behavior of the elements. The values of attributes must always be placed within double quotes. The syntax will be like this: Syntax: There are multiple ways of changing the background color in JavaScript, all of which are discussed in this article. Methods to change background color
Utilizing the style.backgroundColor propertyA simple way to change the background color of an element is by changing its style property. The property allows changing the direct CSS attributes of an element through JavaScript. Syntax Parameters:
Code Output ![]() At the time, when we have clicked on "Click here to change the background color" we will see how the background color changed to pink and it is as seen below: ![]() Employing classList.add()You can use classList.add() to add a CSS class to the div, which will change its background color. Syntax Parameters:
Code Output ![]() Upon clicking the button labeled "Click here to change color of the box," the box's background color will alter to a light blue shade as indicated in the code below. ![]() Applying setAttribute()The setAttribute() function allows you to assign an attribute to an element. It can be used to change the style property instantly. Syntax: Parameters:
Code Output ![]() When we click the button "Press this button to alter the background color" it will appear as below: ![]() Using addEventListener methodJavaScript offers a way to link an event to an HTML element. The addEventListener() method monitors events like clicks, mouse movements, or key presses and runs a specific function whenever the event occurs. Code Output ![]() When we click the button "Press this button to alter the color using addEventListener method" it will show as below: ![]() Conclusion
Next TopicJavaScript Grid |
JavaScript provides a variety of operators to evaluate mathematical and logical operators and expressions in your programs. Increment and decrement operators are two kinds of unary operators in JavaScript that add or subtract a value of 1 from an operand. The purpose of this article is...
3 min read
In JavaScript, we can hide the elements using the style.display or by using the style.visibility. The visibility property in JavaScript is also used to hide an element. The difference between the style.display and style.visibility is when using visibility: hidden, the tag is not visible, but...
2 min read
Sorting is the way in which ordering of elements are presented according to some criteria. In JavaScript, elements of array are sorted, which can be string or number. Sorting makes data more accessible, helps in searching and enables fast access of search. Types of Sorting in JavaScript Alphabetical...
11 min read
Sorting an array of objects in JavaScript is a fundamental operation that programmers often encounter in web development and general software engineering tasks. Understanding how to sort objects based on specific keys is crucial for efficiently organizing and manipulating data in JavaScript applications. In this discussion,...
7 min read
The JavaScript Arrow function does not have the "this" value or any argument. Using it as an event handler, an object literal method, or a prototype method is not recommended. If a function applies the arguments objects, the arrow function does not work correctly. The...
4 min read
We will comprehend how to convert a number to a string in JavaScript in this article. There are many methods that we can utilize to convert a number to string in JavaScript which are as follows: Utilizing the toString() Method Utilizing the String() function Concatenating an Empty String Utilizing the...
3 min read
The strings are usually used to store and manipulate the text data. However, you can also store special characters and numeric data in strings as well. E.g., address or email id, which contains multitype data. Strings are created by putting data inside the quotes. JavaScript and...
3 min read
JavaScript is a programming language used extensively in web development due to its flexibility and power. It works with almost all web browsers Replit is one of the best platforms for learning, practicing and excelling in JavaScript. Replit (IDE) Replit is a cloud-based integrated development environment,...
13 min read
Sorting the arrays of objects in JavaScript is a basic manipulation and is also a really important part of programming which is used in day-to-day web development. JavaScript offers two built-in sorting methods: Array.prototype.sort and Array. Sort that both sort the elements in place, but the...
6 min read
JavaScript is a programming language that allows developers to create dynamic web pages and applications. One of the most important features of JavaScript is its ability to execute code repeatedly, at predefined intervals. In this article, we will discuss how to call a function repeatedly...
3 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India