Generate Hex Code using JavaScript

Here is the quick tip in javascript. This will generate the hex code for you, using just one line of code.

First of all thanks to Paul Irish for such a great code which helps us to get the hex code using JavaScript.

Have a look at below code block for the same.

"#"+Math.floor(Math.random()*16777215).toString(16)
  "#"+Math.floor(Math.random()*16777215).toString(16)

Above line will return the a valid hex code for you.

Now suppose you want to change font color of any p tag then you just need to use below code. First of all give that tag any unique ID and have a look at below code block.

// Considering you have included the MooTools already window.addEvent('domready', function(){ (function(){ var color = '#'+Math.floor(Math.random()*16777215).toString(16); $("highlight").setStyle('color', color )} ).periodical(1000); });
// Considering you have included the MooTools already
window.addEvent('domready', function(){
(function(){
  var color = '#'+Math.floor(Math.random()*16777215).toString(16);
  $("highlight").setStyle('color', color )}
).periodical(1000);

});

Have a look at this demo page which shows this trick in live mode.

Reference

Related Posts

Written by Avinash

Avinash Zala is leading various projects which deals with the various technology involved with the web. A combination of perfect technical and management skills. Avinash would like to chat with you and convert your imagination into the working system. You can get in touch with him on Facebook and Twitter.

View all posts by: