document.bgColor / document.fgColor as you mention will change the document background / foreground colors respectively.
To isolate a single piece (block) of text, wrap it in a DIV and add an "id" to it:
Code:
<style type="text/css">
div.newstyle {
background-color: #ff00ff;
color: #00ff00;
}
</style>
<div id="thetext">This is the text.</div>
<script type="text/javascript">
document.getElementById("thetext").style.backgroundColor = "#0000ff";
document.getElementById("thetext").style.color= "#ff00ff";
</script>
<button onclick="document.getElementById('thetext').className='newstyle';">Change</button>
You can either set the text color directly inline, or (replace the BUTTON with another inline script command) assign a new class dynamically.
NOTE: In this example, the '...("thetext").style...' assignments have higher priority (CSS wise) over the class assignment by the BUTTON. Therefore you would need to add "!important" to your class definition, for this code to work.
Using your example:
You could create a two dimensioned array with background/foreground colors, and then pass the "num" value to reference the correct pair.
...
colors[num][0]='{foreground color}';
colors[num][1]='{background color}';
...
obj=document.getElementById('thetext');
obj.style.color=colors[num][0];
obj.style.backgroundColor=colors[num][1];
...
Hope this helps.
Pete.
Web Developer & Aptrix / Lotus Workplace Web Content Management (LWWCM) Specialist
w:
e: Pete.Raleigh(at)lclimited.co.uk