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.
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.
Share Improve this question asked Nov 1, 2011 at 18:44 rahkimrahkim 9512 gold badges9 silver badges18 bronze badges 4- Show your real html markup. JSFiddle it! – epascarello Commented Nov 1, 2011 at 18:53
- 1 Is your table actually scrolled? Works for me here. – Kraz Commented Nov 1, 2011 at 20:56
- Ive never seen JSFiddle! Very cool. Let me see if I can figure out how to put my code out there. Ive seen plenty of examples that work with scrolling divs or list. Im trying to do a scrolling table with frozen headers. I dont want to put the table in a div as that seems to be a little cumbersome. – rahkim Commented Nov 2, 2011 at 11:40
- I put it up on JSFiddle. For this example, I put an alert in the onclick of the table that returns the scrollTop position. I know this doesn't work, but Im not sure why. jsfiddle/rahkim/TegrJ/8 – rahkim Commented Nov 2, 2011 at 12:39
2 Answers
Reset to default 2As 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/mBDjx/46/
I reend 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!
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.