473,396 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

change bgcolor of td?

Hi,

How can I change the bg color of a table td by javascript? The color is a variable.

e.g.
<head>
<script>
color1="#FF0000";
</script>
</head>

<body>
<table>
<tr>
<td onMouseOver='this.bgColor=color1'>AAA</td>
<td>BBB</td>
</tr>
</table>
</body>

However, I want the color be set when the page is loaded, but not after the mouse events.
Thank you very much.

Frederick

Jul 23 '05 #1
9 8838

"chlaw" <ch***@cse.cuhk.edu.hk> ???????/???????? ? ???????? ?????????:
news:40***************@cse.cuhk.edu.hk...
Hi,

How can I change the bg color of a table td by javascript? The color is a variable.
e.g.
<head>
<script>
color1="#FF0000";
</script>
</head>

<body>
<table>
<tr>
<td onMouseOver='this.bgColor=color1'>AAA</td>
<td>BBB</td>
</tr>
</table>
</body>

However, I want the color be set when the page is loaded, but not after the mouse events. Thank you very much.

Frederick

<script>
color1='#FAFAFA';
color2='#EEEEEE';
coloradd="'"+color2+"'";
document.write('<td STYLE="background-color:'+color2+'"
onMouseOut="this.style.backgroundColor='+"'"+color 2+"'"+'"
onMouseOver="this.style.backgroundColor='+"'"+colo r1+"'"+'">some
text</td>');
</script>
Jul 23 '05 #2

"Dennis Biletsky" <uf***@ua.fm> ÓÏÏÂÝÉÌ/ÓÏÏÂÝÉÌÁ × ÎÏ×ÏÓÔÑÈ ÓÌÅÄÕÀÝÅÅ:
news:c9*********@fortress.intercom.net.ua...

"chlaw" <ch***@cse.cuhk.edu.hk> ???????/???????? ? ???????? ?????????:
news:40***************@cse.cuhk.edu.hk... [snip] <script>
color1='#FAFAFA';
color2='#EEEEEE';
coloradd="'"+color2+"'";
document.write('<td STYLE="background-color:'+color2+'"
onMouseOut="this.style.backgroundColor='+"'"+color 2+"'"+'"
onMouseOver="this.style.backgroundColor='+"'"+colo r1+"'"+'">some
text</td>');
</script>

Sorry "coloradd row" unnecessary. It's mistake ;)
Jul 23 '05 #3
Dennis Biletsky wrote:
"Dennis Biletsky" <uf***@ua.fm> ÓÏÏÂÝÉÌ/ÓÏÏÂÝÉÌÁ × ÎÏ×ÏÓÔÑÈ ÓÌÅÄÕÀÝÅÅ:
news:c9*********@fortress.intercom.net.ua...

"chlaw" <ch***@cse.cuhk.edu.hk> ???????/???????? ? ???????? ?????????:
news:40***************@cse.cuhk.edu.hk...

[snip]
<script>
color1='#FAFAFA';
color2='#EEEEEE';
coloradd="'"+color2+"'";
document.write('<td STYLE="background-color:'+color2+'"
onMouseOut="this.style.backgroundColor='+"'"+color 2+"'"+'"
onMouseOver="this.style.backgroundColor='+"'"+colo r1+"'"+'">some
text</td>');
</script>

Sorry "coloradd row" unnecessary. It's mistake ;)


Thanks, Dennis.

However, in this way, do I need to move all my html tags into "document.write();" statements ?
I want to minimize the modification on my html file...

Frederick

Jul 23 '05 #4
chlaw wrote:
Dennis Biletsky wrote:
"Dennis Biletsky" <uf***@ua.fm> ÓÏÏÂÝÉÌ/ÓÏÏÂÝÉÌÁ × ÎÏ×ÏÓÔÑÈ ÓÌÅÄÕÀÝÅÅ:
news:c9*********@fortress.intercom.net.ua...

"chlaw" <ch***@cse.cuhk.edu.hk> ???????/???????? ? ???????? ?????????:
news:40***************@cse.cuhk.edu.hk...

[snip]
<script>
color1='#FAFAFA';
color2='#EEEEEE';
coloradd="'"+color2+"'";
document.write('<td STYLE="background-color:'+color2+'"
onMouseOut="this.style.backgroundColor='+"'"+color 2+"'"+'"
onMouseOver="this.style.backgroundColor='+"'"+colo r1+"'"+'">some
text</td>');
</script>

Sorry "coloradd row" unnecessary. It's mistake ;)


Thanks, Dennis.

However, in this way, do I need to move all my html tags into "document.write();" statements ?
I want to minimize the modification on my html file...

Frederick


O, I got it now. Just move the line of document.wirte() inside the <tr></tr>... Thanks.

Jul 23 '05 #5
chlaw <ch***@cse.cuhk.edu.hk> wrote in message news:<40***************@cse.cuhk.edu.hk>...
Hi,

How can I change the bg color of a table td by javascript? The color is a variable.

e.g.
<head>
<script>
color1="#FF0000";
</script>
</head>

<body>
<table>
<tr>
<td onMouseOver='this.bgColor=color1'>AAA</td>
<td>BBB</td>
</tr>
</table>
</body>

However, I want the color be set when the page is loaded, but not after the mouse events.
Thank you very much.

Frederick


----------------------------------------------------------------------
Give the desired cell an id name (<TD id="ACELL">xxx</td>

Then, in the javascript, in whatever event you desire (I think you
want it in the onload event in the <BODY> tag) have a line

document.all.ACELL.style.backgroundColor = varColor, where varColor
is your variable color
Jul 23 '05 #6
bruce wrote:
chlaw <ch***@cse.cuhk.edu.hk> wrote in message news:<40***************@cse.cuhk.edu.hk>...
Hi,

How can I change the bg color of a table td by javascript? The color is a variable.

e.g.
<head>
<script>
color1="#FF0000";
</script>
</head>

<body>
<table>
<tr>
<td onMouseOver='this.bgColor=color1'>AAA</td>
<td>BBB</td>
</tr>
</table>
</body>

However, I want the color be set when the page is loaded, but not after the mouse events.
Thank you very much.

Frederick


----------------------------------------------------------------------
Give the desired cell an id name (<TD id="ACELL">xxx</td>

Then, in the javascript, in whatever event you desire (I think you
want it in the onload event in the <BODY> tag) have a line

document.all.ACELL.style.backgroundColor = varColor, where varColor
is your variable color


Thanks! It's actually what I want.

Jul 23 '05 #7
bruce wrote:
chlaw <ch***@cse.cuhk.edu.hk> wrote in message news:<40***************@cse.cuhk.edu.hk>...


<--snip-->
However, I want the color be set when the page is loaded, but not after the mouse events.
Thank you very much.

Frederick

----------------------------------------------------------------------
Give the desired cell an id name (<TD id="ACELL">xxx</td>

Then, in the javascript, in whatever event you desire (I think you
want it in the onload event in the <BODY> tag) have a line

document.all.ACELL.style.backgroundColor = varColor, where varColor
is your variable color


Why not document.getElementById('ACELL').style.backgroundC olor =
so that it works in other browsers as well?

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/
Jul 23 '05 #8
Randy Webb <hi************@aol.com> wrote in message news:<Lt********************@comcast.com>...
bruce wrote:
chlaw <ch***@cse.cuhk.edu.hk> wrote in message news:<40***************@cse.cuhk.edu.hk>...


<--snip-->
However, I want the color be set when the page is loaded, but not after the mouse events.
Thank you very much.

Frederick

----------------------------------------------------------------------
Give the desired cell an id name (<TD id="ACELL">xxx</td>

Then, in the javascript, in whatever event you desire (I think you
want it in the onload event in the <BODY> tag) have a line

document.all.ACELL.style.backgroundColor = varColor, where varColor
is your variable color


Why not document.getElementById('ACELL').style.backgroundC olor =
so that it works in other browsers as well?

My code doesn't recognize any other browsers :) Our systems
assume IE only, so all my answers will too. I should specify that.
Thanks for the additional accuracy.
Jul 23 '05 #9
Dennis Biletsky wrote:
<script>
color1='#FAFAFA';
color2='#EEEEEE';
coloradd="'"+color2+"'";
document.write('<td STYLE="background-color:'+color2+'"
onMouseOut="this.style.backgroundColor='+"'"+color 2+"'"+'"
onMouseOver="this.style.backgroundColor='+"'"+colo r1+"'"+'">some
text</td>');
</script>


This is invalid HTML. The "script" element misses a "type" attribute
(with the value "text/javascript") and the element it must not occur
as child of "table" or "thead", "tbody", "tfoot" or "tr". Do that
server-side. Anything else is considered harmful.

As for the onmouse* event handlers, this is inefficient, unless only
one table cell is going to be highlighted. I think I have posted a
combination of CSS td:hover and an event listener for the parent "table"
element here previously.
PointedEars
Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Boris | last post by:
It is pretty easy to change background color for a text segment of gtk.TextView, or for entire widget. What I cannot find out - how to change bgcolor for entire line, from left to right widget...
5
by: kj | last post by:
Is there a way to dynamically (e.g. upon onClick) change the bgColor attribute of a table cell? I tried cell.setAttribute("bgColor", someColor), where someColor was a string of the form "#RRGGBB",...
15
by: @sh | last post by:
Trying to do this, but its not working...?! My aim being to change the bgcolor of the cell upon which the user has rolled over, I'll then replicate this as an OnMouseOut to change it back. ...
7
by: Cliff Cavin | last post by:
How do I programatically change the background color of an aspx page in code? Is there something like Page.BgColor = "#FFFFFF" ? For that matter, how do I access any of the DOCUMENT...
1
by: Charles Shao | last post by:
How to chang bgcolor of row when mouse selected a row? I have a HTML table in the page. I hope to change <tr.bgcolor> when user clicks on a column, just like user has selected this row. How...
6
by: acord | last post by:
Hi, I want to change the background color of a ul/li row, but the following code is not working as I expected. It works in <table><tr>... <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.