By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,853 Members | 1,563 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,853 IT Pros & Developers. It's quick & easy.

dynamically changing background color HELP please

P: n/a
Hello,
I'm having a problem dynamically changing the color of a table background.

I'm not sure exactly how to word this, but I'll give an example.

I have a function called greentored(propname)

i want to be able to change the following to a different color:
document.all.propname.style.visibility

but substitute the propname with the variable that I'm sending over.

Here is what I had written before but it did not work, it should be easy to
understand what i'm trying to do:

function greentored(propname) {

changecolor = "document.all." + propname + ".style.background";

changecolor = "green";

}

I understand why this doesn't work because all i'm doing is reasigning
"changecolor" to equal green, rather then setting the property of the first
instance of "changecolor" to green. I hope I explained this well enough.

Thanks,

Billy
Jul 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
In article <10*************@corp.supernews.com>, bi***@jimtown.org
enlightened us with...
Hello,
I'm having a problem dynamically changing the color of a table background.

And it only works in IE, too.
I'm not sure exactly how to word this, but I'll give an example.

I have a function called greentored(propname)

i want to be able to change the following to a different color:
document.all.propname.style.visibility


That is IE only syntax.
Not all of us use IE.

Visibility has no color. It is a property all by itself
(style.visibility). Most block elements have a style.backgroundColor
property. Some block elements, notably those with text like P and DIV,
have a style.color property.

I will assume that what you are sending is the name of an element.
For this to be cross-browser, what you need is an id.
I will show you a function that is for DOM browsers only (IE5+/NN6
+/Mozilla/Opera6+ etc). You're on your own for NN4 - I wiped my hands on
that one.

For this example, I have an element that has a background color
property.
<div id="myId">my div contents</div>
The function would then be called as
greenToRed("myId");

The function is defined in the head of the document as
(Watch for word-wrap)

function greenToRed(propname)
{
var changeColor="green";
if (document.getElementById(propname) && document.getElementById
(propname).style)
{
document.getElementById
(propname).style.backgroundColor=changeColor;
}
}

HTH

--
--
~kaeli~
Found God? If nobody claims Him in 30 days, He's yours to
keep.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 20 '05 #2

P: n/a
Thanks.... I've just started working on an addition to my website and you
reminded me i
need to test on Netscape as well as IE. yes, i did give my example wrong...
i
did mean "style.background" but your help was helpful none the less.
Thanks.

Billy

"kaeli" <ti******@NOSPAM.comcast.net> wrote in message >
And it only works in IE, too.
I'm not sure exactly how to word this, but I'll give an example.

I have a function called greentored(propname)

i want to be able to change the following to a different color:
document.all.propname.style.visibility


That is IE only syntax.
Not all of us use IE.

Visibility has no color. It is a property all by itself
(style.visibility). Most block elements have a style.backgroundColor
property. Some block elements, notably those with text like P and DIV,
have a style.color property.

I will assume that what you are sending is the name of an element.
For this to be cross-browser, what you need is an id.
I will show you a function that is for DOM browsers only (IE5+/NN6
+/Mozilla/Opera6+ etc). You're on your own for NN4 - I wiped my hands on
that one.

For this example, I have an element that has a background color
property.
<div id="myId">my div contents</div>
The function would then be called as
greenToRed("myId");

The function is defined in the head of the document as
(Watch for word-wrap)

function greenToRed(propname)
{
var changeColor="green";
if (document.getElementById(propname) && document.getElementById
(propname).style)
{
document.getElementById
(propname).style.backgroundColor=changeColor;
}
}

HTH

--
--
~kaeli~
Found God? If nobody claims Him in 30 days, He's yours to
keep.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace


Jul 20 '05 #3

P: n/a
In article <10*************@corp.supernews.com>, bi***@jimtown.org
enlightened us with...
Thanks.... I've just started working on an addition to my website and you
reminded me i
need to test on Netscape as well as IE.


Don't forget Opera. :)

And ask around for people to test with Safari and other browsers they
may have, especially IE for Mac.

--
--
~kaeli~
Never mess up an apology with an excuse.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.