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
Solved

onmouseover event to change text color in a JS created table

Posted on 2007-04-03
3
1,166 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

When writing CSS, there are a few simple rules that will make your life easier.    1. Using ‘* {box-sizing:border-box;}’. Using this will wrap all your elements in a nice little compact box-model that will give you the width you want, like so... …
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

860 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