Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

select range of table cells

Status
Not open for further replies.

plectrum

Programmer
Feb 16, 2004
21
0
0
US
Hi,
What I am trying to do here is to select number of cells in a table row then do something else on a click. The enclosed code is what's doing. From UI point of view, onmousedown is a better choice to select the cells instead of using mouseover, but the mousedown doesn't work in this code because the mousedown event can't be carried over to next cell. Any pointers? Thx in advance..

------------------------------------------------------------
<html>
<head>
<script>

var tableColumns;
var highlightColor;
var start = 0;
var finish = 0;



function tableInit ( columns, highlight )
{
tableColumns = columns;
highlightColor = highlight;
var column;

document.writeln('<TABLE border="0" cellpadding="0" cellspacing="0" ><TBODY>');
document.writeln('<TR >');
for ( column = 1; column <= columns; column++ )
{
document.writeln('<TD id="' + column + '" onmouseover="checkHighlight( this.id );" onclick="submitSelection( this.id );" width="35" height="35" style="background:silver;" >'+column+'</TD>');
}
document.writeln('</TR>');
document.writeln('<TR><TD id="tableSize1" align=left style="border:1px solid #808080" height="15"> 0 </TD>');
document.writeln('<TD align=center style="border:0px solid #808080" height="15"> - </TD>');
document.writeln('<TD id="tableSize2" align=right style="border:1px solid #808080" height="15"> 0 </TD></TR></TBODY></TABLE>');
}

function checkHighlight ( id )
{
var column;
var tmp_a;
var tmp_b;


if (start == 0 )
{
start = id;
}
finish = id;
tmp_a = parseInt(finish) <= parseInt(start) ? parseInt(finish):parseInt(start)
tmp_b = parseInt(finish) > parseInt(start) ? parseInt(finish):parseInt(start)

for ( column = 1; column <= tableColumns; column++ )
{
document.getElementById( column ).style.backgroundColor = "silver";
}
for ( column = tmp_a==0 ? tmp_a+1: tmp_a; column <= tmp_b; column++ )
{
document.getElementById( column ).style.backgroundColor = highlightColor;
}

document.getElementById( 'tableSize1' ).innerHTML = tmp_a;
document.getElementById( 'tableSize2' ).innerHTML = tmp_b;
}

function submitSelection ( id )
{
var a, b;
a = start;
b = finish
start = 0;
finish = 0;
// Put whatever you want in here.
alert ( a + ' : ' + b);

}

tableInit ( 31, 'cyan' );

</script>
</head>
<body>
</body>
</html>
----------------------------------------------------------
 
BTW, seems this code acts differently IE, but still...( I am using mozilla)

plectrum
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top