Jagd
Jagd

Reputation: 7307

jQuery and how scroll table to a certain row

I have an HTML table that contains some 500 rows. I have an input textbox on the page where the user can enter some text to search for in the table rows, and when they click the Enter button the page executes some jQuery to find and highlight the row that contains the text they entered. The highlight is nothing more than changing the backcolor of the row, of course.

QUESTION Assuming that the row that ends up highlighted is further down (or above) the user's current view of the table, how do I scroll the page so that the newly highlighted row comes into view?

I tried the following for testing, but it's not working. (Just want to scroll the webpage to the last row in the table).

var rowPos = tbl.find('tr:last').position();
$("form:first").scrollTop(rowPos.top);

Thanks for the help!

Upvotes: 2

Views: 3734

Answers (2)

Kundan Singh Chouhan
Kundan Singh Chouhan

Reputation: 14282

try this

$("html,body").animate({scrollTop: rowPos.top}, 800);

Upvotes: 1

GregM
GregM

Reputation: 2654

You should try this:

 window.scrollTo(0,200);

or this if you want to use jquery :

$("html").scrollTop();

Be sure that it's your window that have the scrollbar

Upvotes: 2

Related Questions