Wednesday 27 November 2013

Randomly Generate Html Color

Are you fed up of copying Hex color code and changing it again and again and do you want to Generate Html Color code randomly, you can acheive this using following javascript code.

<script>
    function get_random_color() {
        var letters = '0123456789ABCDEF'.split('');
        var color = '#';
        for (var i = 0; i < 6; i++ ) {
            color += letters[Math.round(Math.random() * 15)];
        }
        document.getElementById("in").setAttribute('style','background-color:' +color);
    }

</script>

I am generating the color randomly on click of button.

<div id="d">
  <input type="text" id="in"/>
  <input type="button" onClick="get_random_color();" value="generate"/>

</div>


No comments:

Post a Comment