views:

39

answers:

1

I have a div with the overflow: auto style. It is populated via AJAX with a result table. The rows in this div are editable, and with each edit I refresh the contents. The problem is, if the user edits something at the bottom of the div, the refresh brings them back to the top.

I had some luck using anchors with Firefox, but not all versions of IE jumped to the anchors within the div.

Is there any way to replace the contents of a div like this without having the scrollbar jump to the top? Any other suggestions? I like to refresh the entire set of results rather than just the updated row if possible, but if there are no workarounds I guess I will pursue that instead.

+2  A: 

Before you change the data, store the scrollTop-property and after that, restore it.

Like so:

var oldScrollTop = div.scrollTop;
div.innerHTML = "new content";
div.scrollTop = oldScrollTop;
edwin
Good idea. In my situation it's a little tricky since the number of records being returned on refresh is not guaranteed to be the same i.e. the scrollbar may need to be a different # pixels from the top.I discovered that using on-page anchors does in fact work in IE just as with Firefox, except that they cannot link to <tr> elements. I moved the unique row IDs to the first <td> within each row and it worked.
RenderIn