How to get Random HEX Color with JavaScript

Use JavaScript to generate a random HEX Color Code

function random_color() {
var letters = '0123456789ABCDEF'.split('');
var color = '#';
for (var i = 0; i < 6; i++ ) { color += letters[Math.round(Math.random() * 15)]; } return color; }

Use jQuery

You could also use jQuery to apply the random color to your element 😉

$('#my_id').css('color', random_color());

About Rick

Senior Front-end Software Engineer from Barcelona, Haidong Gumdo Instructor (korean martial art of the sword), street photographer, travel lover, TV addict, Boston Red Sox fan, and privacy advocate.

Leave a Reply

Add <code> Some Code </code> by using this tags.