View Full Version : help needed...

12-17-2005, 04:06 AM
Currently i am trying to figure out how to change background color of a document and the text color with the click of a button, but so far i can only get the background color to change :\ heres what im using...


<body text="black">
<INPUT type="button" value="Change bg and text color" name="button3"

onClick="document.bgColor='yellow'" onmousedown="document.text='red'">

any ideas on how to get the text color to change when the button is clicked?

12-17-2005, 07:09 AM
This should do it:
onClick="document.bgColor = 'yellow'; document.fgColor='red';"

12-17-2005, 10:42 AM
No, no it shouldn't.
<style type="text/css">
body {
color: black;
/* always specify a background colour when you specify a foreground colour. */
background-color: white;
<form action=""> <!-- The "action" attribute is required. -->
value="Change BG and text color"
// There is no need to use more than one event for multiple statements.
// Seperate them with semicolons (;) and/or linebreaks.
</html>I suggest you run your code through a validator (http://validator.w3.org/). I also recently gave some advice (http://www.dynamicdrive.com/forums/showpost.php?p=23146&postcount=1337) about laying out your code nicely, which I suggest you follow.

12-18-2005, 02:23 AM
whats it really matter though if the source is validated and indented? as long as a code works fine in internet explorer and firefox im satisfied

12-18-2005, 04:44 AM
I was surprised that this:

onClick="document.bgColor = 'yellow'; document.fgColor='red';"

actually worked. Never heard of fgColor before, learn something old every day. This really doesn't seem as though it would be very cross browser friendly. But, sometimes the oldest methods (I'm assuming this is an old method) are the best when it comes to that. What browsers won't this work on, Twey?

12-18-2005, 01:22 PM
I think it's probably rather cross-browser friendly; bgColor at least I seem to remember being around before CSS was in wide usage, and if it's now still supported, it's probably fairly widely-instated. However, it is not, by any stretch of the imagination, forward-compatible, and so should be avoided at all costs (except perhaps as a safety-net for older browsers). Not being backwards-compatible is bad; not being forwards-compatible is worse.

12-18-2005, 04:30 PM
Never heard of fgColor before, learn something old every day.There are several colour-related properties on the document object, however they were all deprecated for properties on the body element, which were in turn deprecated for CSS properties.