• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / JavaScript Random Color – How to Generate a Random Color In JavaScript

JavaScript Random Color – How to Generate a Random Color In JavaScript

September 8, 2022 Leave a Comment

To generate a random color using JavaScript, the simplest way is to use the JavaScript random() method along with the Math.floor() and toString() methods.

var randomColor = "#" + (Math.floor(Math.random()*16777215).toString(16));

We can wrap this code in a function so that we can reuse this function anytime we want a new random color.

function randomColor(){
  return ("#" + (Math.floor(Math.random()*16777215).toString(16)));
};

Let’s break down the code above.


First, you will notice we use the random() method which will return a float between 0 and 1. We multiply this by the number 16777215 because that is the total possible combinations of RGB(255,255,255).

We use the Math.floor() method to round our float down to an Integer. We finally use the toString() method with base 16 to convert our number to a hex. We add # to the front of our hex so that the color is ready to use.

Generating a Random Color on Click Using JavaScript

To generate a random color on click using JavaScript, we can combine our randomColor() function above with a click event.

Let’s say we have the following HTML, and we want to give the user the ability to generate a random color and show the random color in a div box below.

<style>#box{ width: 50px; height: 50px; margin-top: 10px;}</style><div id="div1">
  <div id="click-me" onclick="randomColor()">Generate random color</div>
  <div id="box"></div>
</div>

We can utilize the onclick event, Math random() method, the Math floor() method, and the toString method to generate a random color and show it to the user.

We will finally use the backgroundColor property to change the background color of our box div.

Below is the JavaScript code which will allow the user to be able to update the background color of the box with a randomly generated color.

function randomColor(){
  var randomColor = "#" + (Math.floor(Math.random()*16777215).toString(16));
  document.getElementById("box").style.backgroundColor = randomColor;
};

The final code and output for this example of how to generate a random color on click using JavaScript is below:

Code Output:

Generate random color

Full Code:

<style>#box{ width: 50px; height: 50px; margin-top: 10px;}</style><div id="div1">
  <div id="click-me" onclick="randomColor()">Generate random color</div>
  <div id="box"></div>
</div>

<script>

function randomColor(){
  var randomColor = "#" + (Math.floor(Math.random()*16777215).toString(16));
  document.getElementById("box").style.backgroundColor = randomColor;
};

</script>

Hopefully this article has been useful for you to understand how to generate a random color using Javascript.

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Replace a Character at an Index
  • 2.  Using JavaScript to Remove Quotes From a String
  • 3.  JavaScript indexOf – Get the position of a value within a string
  • 4.  Using JavaScript to Check if Number is Divisible by Another Number
  • 5.  How to Select All Checkboxes in JavaScript
  • 6.  Using JavaScript to Get New Date Format in dd mm yy
  • 7.  Creating a JavaScript Function to Multiply Two Numbers
  • 8.  JavaScript Trunc with Math.trunc() Method
  • 9.  Using Javascript to Check if Value is Not Null
  • 10.  Using JavaScript to Remove the Last Character From a String

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy