How to Change Color of Element Dynamically in JavaScript

Introduction

In this tutorial we will create a How to Change Color of Element Dynamically in JavaScript. This tutorial purpose is to provide a technique to change the cssstyle dynamically. This will tackle the changing of element color. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you will do it without a problem. This program can be use to any part of your on working project that needed dynamic interaction. I will try my best to give you the easiest way of creating this program Dynamic Element Color Change. So let's do the coding.

Before we get started:

This is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface for our application. This code will display the elements and the rest of interactive buttons. To create this simply copy and write it into your text editor, then save it as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  6.         </head>
  7.         <nav class="navbar navbar-default">
  8.                 <div class="container-fluid">
  9.                         <a class="navbar-brand" href="https://www.sourcecodester.com">Sourcecodester</a>
  10.                 </div>
  11.         </nav>
  12.         <div class="container-fluid">  
  13.                 <div class="row">
  14.                         <div class="col-md-3"></div>
  15.                         <div class="col-md-6 well">
  16.                                 <h3 class="text-primary">How to Change Color of Element Dynamically</h3>
  17.                                 <hr style="border-top: 1px SOLID #8c8b8b;"/>
  18.                                 <div class="col-md-3" style="padding:10px;">
  19.                                         <button id="red" onclick="changeColor(this.id)">Red</button>
  20.                                         <br /><br />
  21.                                         <button id="blue" onclick="changeColor(this.id)">Blue</button>
  22.                                         <br /><br />
  23.                                         <button id="green" onclick="changeColor(this.id)">Green</button>
  24.                                         <br /><br />
  25.                                         <button id="yellow" onclick="changeColor(this.id)">Yellow</button>
  26.                                         <br /><br />
  27.                                         <button id="orange" onclick="changeColor(this.id)">Orange</button>
  28.                                         <br /><br />
  29.                                         <button id="purple" onclick="changeColor(this.id)">Purple</button>
  30.                                         <br /><br />
  31.                                         <button id="pink" onclick="changeColor(this.id)">Pink</button>
  32.                                         <br /><br />
  33.                                         <button id="black" onclick="changeColor(this.id)">Black</button>
  34.                                 </div>
  35.                                 <div class="col-md-6" id="box" style="border:1px solid #000; height:280px;">
  36.                                        
  37.                                 </div>
  38.                                 <div class="col-md-3">
  39.                                        
  40.                                 </div>
  41.                         </div>
  42.                 </div>
  43.         </div> 
  44. </body>
  45. <script src="script.js"></script>
  46. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will only cover the changing of element color. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. function changeColor(id){
  2.         document.getElementById("box").style.backgroundColor=id;
  3. }

In this code we only create a method called changeColor(), and putting the button id in order to get the value. We then targeted the element id and use style function to get the attribute background color, we apply the button value to change the element color.

Output:

The How to Change Color of Element Dynamically in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Change Color of Element Dynamically in JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment