views:

74

answers:

2

I wrote a javascript code displaying the date. How would I change the color?

+5  A: 

You need to put the text in a separate element, then change the element's color CSS property.

This is easiest to do using jQuery:

<span id="date">Please enable JavaScript</span>

$('#date').text(new Date().toString()).css('color', 'red');

However, you might want to do it with pure CSS:

(In the head tag:)

<style type="text/css">
    #date {
        color: red;
    }
</style>
SLaks
Your 'l' at the end should be a semicolon :-)
Topher Fangio
+5  A: 

You could set inline CSS properties of the element where you display the date, by using the element.style property:

var el = document.getElementById('elementId');
el.innerHTML = date; // set the text
el.style.color = '#ff0000'; // set the text color

Or you could apply a CSS class programmatically:

el.className = 'yourclass';
CMS