Solved

onmouseover event to change text color in a JS created table

Posted on 2007-04-03
3
1,148 Views
Last Modified: 2012-06-21
I have a HTML table with many rows! When I mouseover "td", I want to change the text color inside the "td".

Problem is that the HTML table is designed in Javascript using:

a = document.createElement("table")
a.id = "my_tbl";

How is it possible to add onmouseover event for "td" of this table "my_tbl" that changes the "td's" text color ?!

Quite urgent.. Thanks :)
0
Comment
Question by:ldbkutty
  • 2
3 Comments
 
LVL 30

Assisted Solution

by:VirusMinus
VirusMinus earned 200 total points
ID: 18843367
hmm.. easiest way i can think of is to download this file which allows CSS behavior of :hover to extend to other elements for IE 6.

http://www.xs4all.nl/~peterned/htc/csshover2.htc

put the file in the root folder of your site.

and then in your CSS add:

body{BEHAVIOR: url(csshover.htc)}
#my_tbl td:hover{background:red;}

ref: explanation about csshover.htc here: http://www.xs4all.nl/~peterned/csshover.html
0
 
LVL 30

Expert Comment

by:VirusMinus
ID: 18843376
then again, it may not work for javascript created elements.

where are you adding the cells? in that code you can use onMouseOver for the cells
0
 
LVL 28

Accepted Solution

by:
TName earned 300 total points
ID: 18843762
As VirusMinus said, or have a look at this and see if it is what you are looking for:


<html>
<head>
<script>

function detour(thing){
   var temp = thing;
   return function(){
    mouseOverCell(temp)
   }
}

function mouseOverCell(obj)
{
  obj.style.color='#f00';
}

function makeTable() {  

  var tbl=document.createElement("TABLE");
  tbl.border=1;
  tbl.cellSpacing=1;
  tbl.id = "my_tbl";
  var r=document.createElement("TR");
  var cell1=document.createElement("TD");
  cell1.innerHTML="Test 1";
  cell1.onmouseover=detour(cell1);
  var cell2=document.createElement("TD");
  cell2.innerHTML="Test 2";
  cell2.onmouseover=detour(cell2);
  r.appendChild(cell1);
  r.appendChild(cell2);
  var tbd=document.createElement("TBODY");
  tbd.appendChild(r);
  tbl.appendChild(tbd);
  document.body.appendChild(tbl);
}

</script>
</head>
<body>
<input type="button" onClick="makeTable()">
<br>
<br>
</body>
</html>

See explanation here:
http://ncyoung.com/archive.php?startDate=2005-7-1&endDate=2005-8-1  ("closures for event handlers with parameters")

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
So you have coded your own WordPress plugin and now you want to allow users to upload images to a folder in the plugin folder rather than the default media location? Follow along and this article will show you how to do just that!
In this tutorial viewers will learn how to style rounded corners for elements in CSS using the border-radius property Begin with a normal styled element such as a div: To style all four corners of the div to be the same degree of roundness, use the …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now