Solved

onmouseover event to change text color in a JS created table

Posted on 2007-04-03
3
1,178 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

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!
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

717 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