9

I want to scroll the html table to particular tr by using Javascript or jquery. Currently I can get the offset of the selected tr .And I am using the scrollTop method .I have tried the following but it is not working for me :

var table  = document.getElementById("table");
var tr = table.getElementsByTagName("tr")[3];
var scrollTo = tr.offsetTop;
table.scrollTop = scrollTo;

also I tried with jquery :

$('#table').animate({scrollTop:0},50);

can anybody help me where am I getting wrong ?

sudo bangbang
  • 27,127
  • 11
  • 75
  • 77
V-Xtreme
  • 7,230
  • 9
  • 39
  • 79

3 Answers3

18

This worked for me, try this

var elm = document.getElementById(id);
elm.scrollIntoView(true);
R. Zagórski
  • 20,020
  • 5
  • 65
  • 90
4

try this : http://jsfiddle.net/SZKJh/

var w = $(window);
var row = $('#tableid').find('tr').eq( line );

if (row.length){
    w.scrollTop( row.offset().top - (w.height()/2) );
}

reference :

https://stackoverflow.com/a/7853216/1982680

Community
  • 1
  • 1
Dipali Vasani
  • 2,526
  • 2
  • 16
  • 30
0

Here the simple step for scroll top function

 var s = $("table tbody > tr:nth-child(20)").position();
 $( "div" ).scrollTop( s.top );

Here the Demo

Abdul Saleem
  • 10,098
  • 5
  • 45
  • 45
Selvamani
  • 7,434
  • 4
  • 32
  • 43