PDA

View Full Version : Change font face, font color and background color using JavaScript code


Plecker
04-21-2003, 06:28 PM
I am trying to change the fontface (example times new roman to another font). I am also trying to change the text color and the background color using JavaScript. I can do this in HTML but am having difficulty making these changes in JavaScript.:o

ConfusedOfLife
04-21-2003, 08:29 PM
It's CSS that can change these stuff and Javascript can only get/change CSS properties. Look at this example:


<p id="oopse">Hello</p>

<script>
myObj = document.getElementById("oopse");
myObj.style.font = "Tahoma";
myObj.style.color = "red";

// Change anything else that you like!
</script>

Roy Sinclair
04-21-2003, 09:13 PM
Diane,

The above code is a good example except it neglects one small detail. If the CSS name for an attribute contains a "-" embedded then the JS name for the same attribute will remove the "-" and then capitalize the first letter of the next word. For example that makes the CSS attribute "background-color" into the Javascript attribute "backgroundColor".