0

As you can see in the code snippet below, I've created a simple colored box in red using HTML and CSS.

I want to create a Javascript file, that checks the current day of the month:

  1. If today is the 1st day of the month it should display the box in it's default color.
  2. If there's a different day, it should multiply the day number with a certain hex number and then add it up to the value in CSS.

I do not know if adding up something to a HEX number is possible, but I guess it should be. Can someone help me with this?

div {
  background: linear-gradient(#C30000, #7E0000);
  height: 160px;
  width: 250px;
}
<div></div>
Otaku Kyon
  • 415
  • 1
  • 9
  • 19
  • You might as well use *only* JavaScript to set the color of that `div`... Anyway, yes, doing mathematical operation on hexadecimal number is possible in JavaScript. Check http://www.w3schools.com/js/js_numbers.asp for the syntax of representing hex – digawp May 30 '15 at 02:35

3 Answers3

1

Since the color of the div will change everyday anyway, I suggest you to set the color of the div only in JavaScript instead of via CSS to prevent complication and undesired behaviour.

To set the color of an HTML element:

document.getElementById("your-div-id").style.background = color;

where color is a string, either a common color name ("blue", "black", "white" etc) or a hexadecimal string ("#FFF", "#123456", etc).

Don't forget to give your div an id first. (eg <div id="my-div"></div>)

As I mentioned in the comment, it is possible to declare a hex number in JavaScript, but internally it will still be stored in decimal. It is not an issue though, but just take note when you are debugging.

To convert it to a hex string (taken from here):

var colorInHexString = colorInHexNumber.toString(16);

And because the HTML only accepts hex string with # prepended, so don't forget to do that.

In case you do not know how to get the day of the month, you can get it from here.

tl;dr

So the full code:

<div id="my-div"></div>
<script>
    var defaultColor = 0xC30000;
    var multiplier = 0x20; // for example
    var dayNumber = new Date().getDate() - 1; // first day of month uses defaultColor
    var todayColor = defaultColor + dayNumber * multiplier;
    var todayColorString = "#" + todayColor.toString(16);
    document.getElementById("my-div").style.background = todayColorString;
</script>

Hope this helps.

Community
  • 1
  • 1
digawp
  • 324
  • 2
  • 13
  • A perfect answer, even though you missed that in my given code snippet was a gradient. But thank you a lot! – Otaku Kyon May 30 '15 at 04:11
0

You can do this in pure JavaScript with:
var _someColor = '#7E0000'; // for example
document.getElementById("__DIV__NAME__").style.background = _someColor;

Docs

jQuery has a function that does the same thing. Can also use the callback to do your calculation of color.

Edit: Don't forget you can use rgb(value,value,value) as well, and can make calculations a bit easier.

Steven Jacks
  • 313
  • 1
  • 2
  • 14
Sze-Hung Daniel Tsui
  • 2,282
  • 13
  • 20
0

you can try something like this.

$('#qaz').css('Color') by using this you only get the rgb color code format

by using javascript function we can get the hex code for rgb

function rgb2hex(rgb){
 rgb = rgb.match(/^rgba?[\s+]?\([\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?/i);
 return (rgb && rgb.length === 4) ? "#" +
  ("0" + parseInt(rgb[1],10).toString(16)).slice(-2) +
  ("0" + parseInt(rgb[2],10).toString(16)).slice(-2) +
  ("0" + parseInt(rgb[3],10).toString(16)).slice(-2) : '';
}

and you convert into hex code rgb2hex($('#qaz').css('Color')) then you can multiply into number and set the color by using css style properties.

Balaji Marimuthu
  • 1,940
  • 13
  • 13
  • OP may not be allowed to use jQuery (since it is not mentioned that OP uses jQuery). Of course, your `rgb2hex` function is still valid if you were to extract the color of the `div` using JavaScript. – digawp May 30 '15 at 03:53
  • Op means, yes you can extract the Color by using query then convert it – Balaji Marimuthu May 30 '15 at 04:08