WebMasterCampus
WEB DEVELOPER Resources

Javascript Randomly Generate Html Hex Color Codes

Learn using Javascript passing an array as a function parameter


Javascript Randomly Generate Html Hex Color Codes

Code Example 1

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

Code Example 2

    randomColor = '#'+(0x1000000+(Math.random())*0xffffff).toString(16).substr(1,6);

Develop Random Hex Code Generator

    <div id="codePanel"></div>
    <button id="numberGenerate">Generate Random Color</button>

    <script>
        function getRandomColor(){
            let randomColor = "#" +  Math.floor( Math.random() * 16777215).toString(16);

            let codePanel = document.getElementById("codePanel");
            codePanel.innerHTML = randomColor;
            codePanel.style.backgroundColor = randomColor;
        }

        let button = document.getElementById("numberGenerate");
        button.addEventListener('click',getRandomColor);

        getRandomColor();
    </script>

    <style>
        #codePanel {
            width: 200px;
            height: 200px;
            border: 1px solid;
        }
    </style>
Created with love and passion.