
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to return the background color of an element with JavaScript DOM?
In this tutorial, we will explore the method by which we can get the background color of an element with JavaScript. There are many scenarios where we may want to get the color of an element on the page, and we will see how to achieve that easily using inbuilt methods provided by JavaScript.
We cannot only retrieve the color of the color we can also set the color of an element using the same function. To achieve this, we will be using a modification of the same function.
Browser Support
Chrome Browser − Version 1.0 and above.
Edge Browser − Version 12 and above.
Internet Explorer − Version 4.0 and above.
Firefox Browser − Version 1.0 and above.
Safari Browser − Version 1.0 and above.
Opera Browser − Version 3.5 and above.
Style.backgroundColor is an in built function in the style library which we can use to set a specific color for an element and retrieve the color of an element as well.
Syntax
object.style.backgroundColor = "color|transparent|initial|inherit"
It can take the following types of four parameters namely −
color − This is responsible for the background color.
transparent − The background color will be transparent.
initial − Changes and sets the current property as the default property.
inherit − It inherits its property from its parent’s element and sets it to it.
Example 1
Let us take a look at how to set a color for an element using the in-built function backgroundColor from the style library.
<!DOCTYPE html> <html> <body> <button onclick="display()">Click to Set background Color</button> <script> function display() { document.body.style.backgroundColor = "blue"; } </script> </body> </html>
In the above code we created a button which when clicked will call another function with the name display which will use the backgroundColor from the style library to set the color of that back ground to blue.
Example 2
Let us take a look at another example where we will only change the color of a div or element and not the whole screen.
<!DOCTYPE html> <html> <body> <h1 style="color:black;">TUTORIAL POINT</h1> <button onclick="change_color()">Click</button> <div id="my-div" style= "width: 250px; height: 100px; background-color: cyan;"> <h1>TutorialPoint</h1> </div> <script> function change_color() { document.getElementById("my-div").style.backgroundColor = "purple"; } </script> </body> </html>
In the above code we created a div with some size and set the current background color to cyan and a button which when clicked will call another function with the name change_color which will as the name suggests change the background color by using the backgroundColor from the style library to set the color of that div back ground to purple from cyan.
Style.backgroundColor is an in built function in the style library which we can use to set a specific color for an element and retrieve the color of an element as well.
Example 3
Let us take a look at how we can retrieve a color from an element present in the web page.
<!DOCTYPE html> <html> <body> <h1 style="color:black;">TUTORIAL POINT</h1> <button onclick="name_color()">Click</button> <div id="my-div" style= "width: 250px; height: 100px; background-color: cyan;"> <h1>TutorialPoint</h1> </div> <script> function name_color() { document.write(document.getElementById("my-div").style.backgroundColor) } </script> </body> </html>
In the above code we created a div with some size and set the current background color to cyan and a button which when clicked will call another function with the name name_color which will as the name suggests tell us the background color using the backgroundColor from the style library to retrieve the color of that div and writing the color on the screen using document.write.
Conclusion
In this tutorial, we saw how we can use a function from the libarty style namely backgroundColor to set a background color for an element present in the web page, we can also use backgroundColor to return or retrieve the background color that has been set for a specific element present in the web page. Combining the style.backgroundColor with a function we can also change the color of the element when a button is pressed.
- Related Articles
- Set the background color of an element with CSS
- How to display the background color of an element in HTML?
- How to return the color of the text with JavaScript?
- How to set the color of the outline around an element with JavaScript?
- How to verify the color and background color of a web element in Selenium?
- Add a red background color to an element to set danger action with Bootstrap
- Add a blue background color to an element to set key stuff with Bootstrap
- Add a green background color to an element to set positive action with Bootstrap
- Add a yellow background color to an element to set warning action with Bootstrap
- How to style background image to no repeat with JavaScript DOM?
- How to create an Ellipse with a background color using FabricJS?
- How to set a background image to be fixed with JavaScript DOM?
- How to set all the background properties in one declaration with JavaScript DOM?
- Set the background image of an element with CSS
- How to choose a background color through a color picker in JavaScript?
