473,513 Members | 2,552 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Generic code to change td color onmousover/out?

I'm changing colors of table cells like this:

<style type="text/css">
..over {background-color: #bedddc}
..out {background-color: #99cccc}
</style>

[snip]
<td onmouseover="this.className='over'" onmouseout="this.className='out'">
[snip]

But what I want is a generic javascript to change the current
onmousover/out cell from a script above
the table instead of using "this.className" within each td code, something
like follows:

<script>
function over(){
???????
}

function out(){
???????
}
</script>

[snip]
<td onmouseover="over();" onmouseout="out();">
[snip]

Can it be done w/out assigning a special id or class to each cell and pass
'this' onto above script for same effect?

If possible - please replace ??????? with any ideas.

Thanks!
michael

--
Those who can't write, write manuals.
Jul 23 '05 #1
3 4756
michael wrote:
Can it be done w/out assigning a special id or class to each cell and pass
'this' onto above script for same effect?
Something like
function over(el) {
el.className="whatever";
}

and then you have
<td onmouseover="over(this)">

:-)

But this isn't generic enough IMHO, it's neater to assign the
mouseover/mouseout handlers outside of the HTML tags, this way you can
prevent the HTML from growing needlessly and also control whether the
handler is supported, making the decision to assign it if it is.

<table id="foo">
<tr><td>Hello World</td></tr>
<tr><td>Hello World</td></tr>
<tr><td>Hello World</td></tr>
</table>

<script type="text/javascript">
window.onload=function(evt){
var d=document, td;

function mover(evt){
this.style.background="yellow";
}

function mout(evt){
this.style.background="";
}

if(d.getElementById &&
d.getElementsByTagName) {

td=d.getElementById("foo").getElementsByTagName("t d");
for(var ii=0;ii<td.length; ii++) {
td[ii].onmouseover=mover;
td[ii].onmouseout=mout;
}
}
}
</script>

would be a good start (you could then add support for document.all,
relatedTarget etc).
If possible - please replace ??????? with any ideas.


Without modifying the calls, that'd be tricky; in IE you'd have to get
the sourceElement from the window.event object, and in other browsers
you'd have to walk up the (non-standard) caller chain until you can
grasp the event of the original handler, finally grabbing the target.
Jul 23 '05 #2
*michael* wrote in comp.lang.javascript:
I'm changing colors of table cells like this:

<style type="text/css">
.over {background-color: #bedddc}
.out {background-color: #99cccc}
</style>

[snip]
<td onmouseover="this.className='over'" onmouseout="this.className='out'">
[snip]

But what I want is a generic javascript to change the current
onmousover/out cell from a script above
the table instead of using "this.className" within each td code

[snip]

I see Yann-Erwan has already replied, however, I was playing around with
the following and thought it might be of some use still (I'm not sure of
it's general suitability - although it does work in IE5.0+, Opera7,
Mozilla/Firefox):
<script type="text/javascript">
function Init() {
AddMouseOverEffect("rootId", "td"); // Your id and tag name here
}

function AddMouseOverEffect(strId, strTag) {
if (document && document.getElementById && document.getElementsByTagName) {
var objRoot = document.getElementById(strId);
if (objRoot) {
var objChildTags = objRoot.getElementsByTagName(strTag);
var intIndex = objChildTags.length;
while (intIndex--) {
var objTag = objChildTags[intIndex];
if (objTag.attachEvent) {
objTag.attachEvent('onmouseover', Effect_MouseOn);
objTag.attachEvent('onmouseout', Effect_MouseOff);
}
else if (objTag.addEventListener) {
objTag.addEventListener("mouseover", Effect_MouseOn, false);
objTag.addEventListener('mouseout', Effect_MouseOff, false);
}
}
}
}
}

function Effect_MouseOn(evnt) {
var context = evnt.srcElement ? evnt.srcElement : this;
if (context && context.style) {
with (context.style) {
backgroundColor = "#bedddc";
color = "#000000";
}
}
return false;
}

function Effect_MouseOff(evnt) {
var context = evnt.srcElement ? evnt.srcElement : this;
if (context && context.style) {
with (context.style) {
backgroundColor = "#99cccc";
color = "#000000";
}
}
return false;
}

if (window.attachEvent) {
window.attachEvent('onload', Init);
}
else if (window.addEventListener) {
window.addEventListener("load", Init, false);
}
</script>

<table id="rootId">
<tr>
<td>Cell 1.1</td>
<td>Cell 1.2</td>
<td>Cell 1.3</td>
<td>Cell 1/4</td>
</tr>
<tr>
<td>Cell 2.1</td>
<td>Cell 2.2</td>
<td>Cell 2.3</td>
<td>Cell 2/4</td>
</tr>
</table>

--
Andrew Urquhart
- FAQ: http://www.jibbering.com/faq/
- Archive: http://www.google.com/groups?q=comp.lang.javascript
- Contact me: http://andrewu.co.uk/contact/
Jul 23 '05 #3
> But this isn't generic enough IMHO, it's neater to assign the
mouseover/mouseout handlers outside of the HTML tags, this way you can


Perfect - thank you!
michael

--
"Deep" is a word like "theory" or "semantic" -- it implies all sorts of
marvelous things. It's one thing to be able to say "I've got a
theory", quite another to say "I've got a semantic theory", but, ah,
those who can claim "I've got a deep semantic theory", they are truly
blessed.
-- Randy Davis

Jul 23 '05 #4

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

Similar topics

6
2232
by: gong | last post by:
hi i recently looked at alexandrescu's book on c++, and i found it pretty much unintelligible. i have a few points which i wonder about. 1. as a developer, it is important, from a bottom line standpoint, to make code as transparent as possible. through its lifecycle, code inevitably becomes larger and more complex to cope with unexpected...
6
18538
by: liglin | last post by:
The following script changes the color of text with the onmousover event. How can it be modified so it changes the text when the button is clicked? I'd want to avoid layers or CSS. Thanks, Liglin <HTML> <HEAD> <TITLE>Untitled Document</TITLE>
17
3290
by: Andreas Huber | last post by:
What follows is a discussion of my experience with .NET generics & the ..NET framework (as implemented in the Visual Studio 2005 Beta 1), which leads to questions as to why certain things are the way they are. ***** Summary & Questions ***** In a nutshell, the current .NET generics & .NET framework make it sometimes difficult or even...
3
1863
by: .Net Newbie | last post by:
I'm new to .Net and need to create a generic (free) way to update lookup tables in SQL Server (using C#) in ASP.Net pages. I found an article at: http://www.dotnetjunkies.com/Tutorial/0C968B48-44E8-4637-9F19-8490B0429881.dcik which explains how this can be done but does not supply enough example code my newbie self to go through in detail...
15
5304
by: David Lozzi | last post by:
Howdy, I have a function that uploads an image and that works great. I love ..Nets built in upload, so much easier than 3rd party uploaders! Now I am making a public function that will take the path of the uploaded image, and resize it with the provided dimensions. My function is below. The current function is returning an error when run...
16
2334
by: tshad | last post by:
This is a little complicated to explain but I have some web services on a machine that work great. The problem is that I have run into a situation where I need to set up my program to access one or another (could also be 3) different web servers to use these Web Services. The Web Services are identical on all the machines. I tried just...
9
12780
by: mps | last post by:
I want to define a class that has a generic parameter that is itself a generic class. For example, if I have a generic IQueue<Tinterface, and class A wants to make use of a generic class that implements IQueue<Tfor all types T (so it can make use of queues of various object types internally). As useful as this is, it doesn't seem possible. The...
26
3591
by: raylopez99 | last post by:
Here is a good example that shows generic delegate types. Read this through and you'll have an excellent understanding of how to use these types. You might say that the combination of the generic delegate type expression in just the right place and a well-named method means we can almost read the code out loud and understand it without even...
0
7270
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7178
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7397
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7128
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7543
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5704
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3255
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3242
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1612
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.