• We just launched and are currently in beta. Join us as we build and grow the community.

How to Change Color of Element Dynamically in JavaScript

Nimja

Kawaii Enthusiast
Divine
N Rep
0
0
0
Rep
0
N Vouches
0
0
0
Vouches
0
Posts
93
Likes
29
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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. <body

    >
  8. <nav

    class

    =

    "navbar navbar-default"

    >
  9. <div

    class

    =

    "container-fluid"

    >
  10. <a

    class

    =

    "navbar-brand"

    href

    =

    "https://www.sourcecodester.com"

    >

    Sourcecodester</

    a

    >
  11. </

    div

    >
  12. </

    nav

    >
  13. <div

    class

    =

    "container-fluid"

    >
  14. <div

    class

    =

    "row"

    >
  15. <div

    class

    =

    "col-md-3"

    ></

    div

    >
  16. <div

    class

    =

    "col-md-6 well"

    >
  17. <h3

    class

    =

    "text-primary"

    >

    How to Change Color of Element Dynamically</

    h3

    >
  18. <hr

    style

    =

    "border-top: 1px SOLID #8c8b8b;"

    /

    >
  19. <div

    class

    =

    "col-md-3"

    style

    =

    "padding:10px;"

    >
  20. <button

    id

    =

    "red"

    onclick

    =

    "changeColor(this.id)"

    >

    Red</

    button

    >
  21. <br

    /

    ><br

    /

    >
  22. <button

    id

    =

    "blue"

    onclick

    =

    "changeColor(this.id)"

    >

    Blue</

    button

    >
  23. <br

    /

    ><br

    /

    >
  24. <button

    id

    =

    "green"

    onclick

    =

    "changeColor(this.id)"

    >

    Green</

    button

    >
  25. <br

    /

    ><br

    /

    >
  26. <button

    id

    =

    "yellow"

    onclick

    =

    "changeColor(this.id)"

    >

    Yellow</

    button

    >
  27. <br

    /

    ><br

    /

    >
  28. <button

    id

    =

    "orange"

    onclick

    =

    "changeColor(this.id)"

    >

    Orange</

    button

    >
  29. <br

    /

    ><br

    /

    >
  30. <button

    id

    =

    "purple"

    onclick

    =

    "changeColor(this.id)"

    >

    Purple</

    button

    >
  31. <br

    /

    ><br

    /

    >
  32. <button

    id

    =

    "pink"

    onclick

    =

    "changeColor(this.id)"

    >

    Pink</

    button

    >
  33. <br

    /

    ><br

    /

    >
  34. <button

    id

    =

    "black"

    onclick

    =

    "changeColor(this.id)"

    >

    Black</

    button

    >
  35. </

    div

    >
  36. <div

    class

    =

    "col-md-6"

    id

    =

    "box"

    style

    =

    "border:1px solid #000; height:280px;"

    >

  37. </

    div

    >
  38. <div

    class

    =

    "col-md-3"

    >

  39. </

    div

    >
  40. </

    div

    >
  41. </

    div

    >
  42. </

    div

    >
  43. </

    body

    >
  44. <script

    src

    =

    "script.js"

    ></

    script

    >
  45. </

    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:

How%20to%20Change%20Color%20of%20Element%20Dynamically%20in%20JavaScript%201.png


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


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,526

323,535

Top