1

Quick disclaimer - I'm a javascript rookie. I'm looking at how to add CSS styling to a javascript variable. Here's a link to my code.

http://jsfiddle.net/Ehney/169/

What I'd like to do is be able to format the dynamic numbers in the counter. Here's the code I used to attempt to do that

var days = document.getElementById("days");
    days.style.color = "blue";

This isn't doing anything though. Please help me. I'd like to know how to add other formatting such as like font sizing too.

Please understand that I am able to add the styles to all of the javascript using the 'countdown' getElementById. But I would like to specifically target the variables. Help!!!

3
  • 1
    Judging by your JSFiddle, there isn't actually an element called Days, which is why you can't assign a colour to it Commented Mar 12, 2014 at 1:00
  • 2
    there is no element with id days in your code Commented Mar 12, 2014 at 1:01
  • You can see in this fiddle that by wrapping your desired text in a span with id="days" you can fix your problem: jsfiddle.net/Ehney/169 I would suggest using a class if you want to do this to the value as well as the Days label. Commented Mar 12, 2014 at 1:03

2 Answers 2

1

Wrap the variables values in a span with id/class

document.getElementById('countdown').innerHTML = 'Days ' + 'Hours ' + 'Minutes ' + 'Seconds  </br> &nbsp <span id="days">' + days + '</span>&nbsp &nbsp &nbsp &nbsp<span id="hours">' + hours + '</span>&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp<span id="minutes">' + minutes + '</span> &nbsp &nbsp &nbsp &nbsp &nbsp<span id="seconds">' + seconds + '</span>';

then define css rules

#days {
    color: blue;
}

Demo: Fiddle

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this worked well. I was able to add whatever CSS styles I wanted
0

You can do it in this way:

var css = document.createElement("style");
css.type = "text/css";
css.innerHTML = "strong { color: red }";
document.body.appendChild(css);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.