View Full Version : highlight table columns
shibby1011ph
10-13-2002, 03:31 AM
does anyone know how to highlight a table column in javascript? given this information: i have table with unknown number of rows but a definite number of columns, say 4 columns. when i select one cell in a particular column, i would like all the cells in that color to be highlighted, how can i do this? please help me!! would gladly appreciate it!!!!
beetle
10-13-2002, 06:12 AM
Hmmm. Easiest way would probably be to use COLGROUPs.<html>
<head>
<title>Column Highlight Test</title>
<style>
colgroup.over {
background-color: #F99;
}
</style>
<script type="text/javascript" language="javascript">
function hiliteColumn(e) {
var o = (document.all) ? e.srcElement : e.target;
if (o.nodeName != "TD") return;
document.getElementById("cg"+(o.cellIndex+1)).className="over";
}
function resetColumn(e) {
var o = (document.all) ? e.srcElement : e.target;
if (o.nodeName != "TD") return;
document.getElementById("cg"+(o.cellIndex+1)).className="";
}
</script>
</head>
<body>
<table border="1" onMouseOver="hiliteColumn(event);" onMouseOut="resetColumn(event);">
<colgroup id="cg1"></colgroup>
<colgroup id="cg2"></colgroup>
<colgroup id="cg3"></colgroup>
<colgroup id="cg4"></colgroup>
<tr>
<td>blah</td>
<td>blah</td>
<td>blah</td>
<td>blah</td>
</tr>
<tr>
<td>blah</td>
<td>blah</td>
<td>blah</td>
<td>blah</td>
</tr>
</form>
</body>
</html>Cheers, and happy coding! :D
hbuisman
05-28-2005, 10:36 PM
After searching google for any code to hilite columns, nothing really did the job. The 'colgroups method' as given above wasn't really sufficient.
Maybe the following works for you, it worked for me, and i tested it in IE and firefox. Id like to hear your comments:
Put the following in the header of your file (or in a js file).
function setColColor(e,table,Color) {
var browserName=navigator.appName;
var q;
if(browserName=="Netscape")
var obj = e.target;
else if(browserName=="Microsoft Internet Explorer")
var obj = e.srcElement;
if(obj.nodeName=='TH'){
// Get the index of TBODY (this is may differ when there are headers etc)
for(var i=0;i<4;i++){
if(table.childNodes[i].nodeName=="TBODY"){
q=i;
break;
}
}
//alert(obj.cellIndex);
// Walk through the rows
for (var i=0; i<table.childNodes[q].childNodes.length; i++)
{
// Walk through the cells, starting (and if html is correctly coded also ending)
// at obj.cellIndex: the col to be hilited
for (var n=obj.cellIndex; n<table.childNodes[q].childNodes[i].childNodes.length; n++)
{
if(table.childNodes[q].childNodes[i].childNodes[n].cellIndex==obj.cellIndex){
table.childNodes[q].childNodes[i].childNodes[n].style.backgroundColor=Color;
break;
}
}
}
}
}
This code is specifically for hiliting colums when the mouse is over the header.
This can simply be changed by changing
if(obj.nodeName=='TH'){
to
if(obj.nodeName=='TD'){
Now put the following in your table tags of the table wich you want column highlighting.
<table onmouseover='setColColor(event,this,"#CD5C5C")' onMouseOut="setColColor(event,this,'')">
(Or as response to a click by using onclick)
This code could be edited to only hilite specific cols. This can be done to check the value of obj.cellIndex
johncoll
07-27-2005, 10:46 PM
Hi hbuisman,
thanks for posting this. It works fine for me under IE but nothing happens at all under firefox 1.0.6. Can you confirm if you have tested it for firefox.
many thanks
john
johncoll
07-27-2005, 10:59 PM
My apologies .... I removed the colgroup statements and it worked fine under firefox as well. oops.
thanks
john
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.