[go: up one dir, main page]

DEV Community

Cover image for JavaScript Infinite Hex Generator
Shafi
Shafi

Posted on • Originally published at shafi.live

JavaScript Infinite Hex Generator

Hexadecimal colors are a way to represent colors using the base-16 number system, which includes the digits 0-9 and the letters A-F. In HTML and CSS, hexadecimal colors are denoted using a hash symbol (#) followed by six hexadecimal digits, which can be either uppercase or lowercase. For example, #FF0000 represents the color red, #00FF00 represents the color green, and #0000FF represents the color blue.

To generate a random hex color with JavaScript, you can use the Math.random() function to generate a random number between 0 and 1, and then use the toString(16) method of the Number object to convert the number to a hexadecimal string.

Let's start generating colors!

  • First, we need to include a button element in our HTML code with an ID of 'generate-color':
   <button id="generate-color">Generate Color</button>
Enter fullscreen mode Exit fullscreen mode
  • Next, we need to include a script element in our HTML code that will contain the JavaScript code for handling the button click event. Inside the script element, we can use the document.querySelector method to select the button element by its ID:
   <script>
     const button = document.querySelector('#generate-color');
   </script>
Enter fullscreen mode Exit fullscreen mode
  • After we have selected the button element, we can use the addEventListener method to attach an event listener to the button. This event listener will be triggered whenever the button is clicked, and it will execute a function called generateColor:
   <script>
     const button = document.querySelector('#generate-color');
     button.addEventListener('click', generateColor);
   </script>
Enter fullscreen mode Exit fullscreen mode
  • The generateColor function will contain the code that generates a random hex color. To verify that our function is indeed generating random colors, we can use the document.body.style.backgroundColor property in our JavaScript code and set the generated color as its value. For example, to set the background color to red, we can use the following code:
   <script>
     const button = document.querySelector('#generate-color');
     button.addEventListener('click', generateColor);

     function generateColor() {
       document.body.style.backgroundColor = 'red';
     }
   </script>
Enter fullscreen mode Exit fullscreen mode

When the button is clicked, the generateColor function will be executed, updating the background color of the web page to red.

  • But we want to update the background to a random color, so we'll update the generateColor function to generate a random color whenever the button is clicked and set the background accordingly:
<script>
  function generateColor() {
    // THIS WILL GENERATE A RANDOM HEX STRING
    const generatedColor = Math.floor(Math.random() * 16777215).toString(16);

    document.body.style.backgroundColor = `#${generatedColor}`;
  }
</script>
Enter fullscreen mode Exit fullscreen mode

Now we know how to generate random hex colors in JavaScript, yay! What about trying to create a color palette generator next time?

Top comments (0)