Solved

onmouseover event to change text color in a JS created table

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

746 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

11 Experts available now in Live!

Get 1:1 Help Now