rahkim
rahkim

Reputation: 941

How do you get/set the scroll position in a table?

I have a table that has a fixed header and a scrollable body. I want to know the scroll position in the table so that I can reference it on postback and set it to where the user was before postback. I would prefer JQuery to do this, but Javascript is fine too. Im not sure how much code to post here because Im not sure if the problem is that I just dont know the code to do this, or the code I have (css) is interfering with the solutions I have tried.

Any ideas where to start? Is there a method that does this? Ive tried putting this (table style="overflow:auto; height:150" onclick="alert(this.scrollTop)") in the table but it always returns 0.

Ive also tried various JQuery calls as well. They all return 0.

Upvotes: 5

Views: 4989

Answers (2)

COBOLdinosaur
COBOLdinosaur

Reputation: 294

It is the tbody that is scrolling and that is what you need to test. this.scrollTop is referring to the table , and the top of the table is not changing position.

Upvotes: 1

ChangePictureWeekly
ChangePictureWeekly

Reputation: 61

As far as I know (and I am dealing with this kind of stuff everyday), native tables can't handle scrollbars as your describing. I think that probably, your table actually contains a div or other container, which is actually scrolling the table cells. Something like this:

http://jsfiddle.net/mBDjx/46/

I recomend you to inspect your code using the browser developer tools (usually F12) and check if it is only a table, or another element the one which is generating the scroll.

If you realize that is is a div, just check the .scrollTop attribute of the div instead of doing it on the table :-)

Hope it works!

Upvotes: 2

Related Questions