views:

2232

answers:

1

I have table inside a div tab. The table has 40 rows in it and the div's height is set to show 10 rows of that table. CSS's overflow:auto lets me scroll through the 40 rows. All is well there.

How can I, with JavaScript cause the table to programatically position to a given row (ie, programmatically scroll the table up or down by row)?

Thank you rp

+11  A: 

Where superHappyFunDiv is the ID of the container DIV and rows is a 0-based row index:

function scrollTo(row)
{
   var container = document.getElementById("superHappyFunDiv");
   var rows = container.getElementsByTagName("tr");

   row = Math.min(Math.max(row, 0), rows.length-1);
   container.scrollTop = rows[row].offsetTop;
}

Will attempt to scroll the requested row to the top of the container. Tested in IE6 and FF3.

Shog9
Thank you very much! Perfect.
rp
rp
Yes - it wouldn't work half as well with any other name.
Shog9