PDA

View Full Version : help needed...



Josh_R
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...


<html><head><title>TEST</title></head>

<body text="black">
<h2>Testing...</h2>
<FORM>
<INPUT type="button" value="Change bg and text color" name="button3"

onClick="document.bgColor='yellow'" onmousedown="document.text='red'">
</FORM>
</font>
</html>

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

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

Twey
12-17-2005, 10:42 AM
No, no it shouldn't.
<html>
<head>
<title>TEST</title>
<style type="text/css">
body {
color: black;
/* always specify a background colour when you specify a foreground colour. */
background-color: white;
}
</style>
</head>
<body>
<h2>Testing...</h2>
<form action=""> <!-- The "action" attribute is required. -->
<input
type="button"
value="Change BG and text color"
name="button3"
onclick="
document.body.style.backgroundColor='yellow';
// There is no need to use more than one event for multiple statements.
// Seperate them with semicolons (;) and/or linebreaks.
document.body.style.color='red';
"
/>
</form>
</font>
</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.

Josh_R
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

jscheuer1
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?

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.

mwinter
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.

Mike