# RJS Move to top of div after Ajax request

**URL:** <https://www.ruby-forum.com/t/rjs-move-to-top-of-div-after-ajax-request/131728>\
**Category:** Rails\
**Created:** [March 12, 2008, 7:28pm UTC](https://www.ruby-forum.com/t/rjs-move-to-top-of-div-after-ajax-request/131728 "2008-03-12T19:28:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [March 12, 2008, 7:28pm UTC](https://www.ruby-forum.com/t/rjs-move-to-top-of-div-after-ajax-request/131728/1 "2008-03-12T19:28:51Z")

</div>

Hi,

Im updating a scrollable div with many links\_to\_remote. If a user has  
scrolled half way down the div and then clicks the link\_to\_remote  
outside the div it then updates the scrollable div but stays half way  
down. I want it to automatically go back to the top of the div.

The code is below for the link to remote, is this possible to do?

\<%= link\_to\_remote “Update Div”,  
:url =\> { :action =\> “load\_questions”, :id =\> category},  
:before =\> “Element.show(‘main\_spinner’)”,  
:success =\> “Element.hide(‘main\_spinner’)” %\>

Any help appreciated.

JB

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [March 12, 2008, 9:15pm UTC](https://www.ruby-forum.com/t/rjs-move-to-top-of-div-after-ajax-request/131728/2 "2008-03-12T21:15:10Z")

</div>

John B. wrote:

> Im updating a scrollable div with many links\_to\_remote. If a user has  
> scrolled half way down the div and then clicks the link\_to\_remote  
> outside the div it then updates the scrollable div but stays half way  
> down. I want it to automatically go back to the top of the div.

Perhaps you could do a javascript “focus” on an element at the top of  
the div in question? I haven’t tried it, so no guarantees, but it may  
work.

Similar to having for use with links that hop around  
the page (common usage for FAQ pages), you could make an empty tag  
with id=“top” and focus on it with javascript.

- Aaron

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [March 13, 2008, 12:05pm UTC](https://www.ruby-forum.com/t/rjs-move-to-top-of-div-after-ajax-request/131728/3 "2008-03-13T12:05:03Z")

</div>

Aaron Brown wrote:

> John B. wrote:
> 
> > Im updating a scrollable div with many links\_to\_remote. If a user has  
> > scrolled half way down the div and then clicks the link\_to\_remote  
> > outside the div it then updates the scrollable div but stays half way  
> > down. I want it to automatically go back to the top of the div.
> 
> Perhaps you could do a javascript “focus” on an element at the top of  
> the div in question? I haven’t tried it, so no guarantees, but it may  
> work.
> 
> Similar to having for use with links that hop around  
> the page (common usage for FAQ pages), you could make an empty tag  
> with id=“top” and focus on it with javascript.
> 
> - Aaron

Thanks for your help,

I created an invisible link at the top of the div and after i update the  
scrollable div i call the code below at the end of the partial:

\<%= javascript\_tag render(:update) { |page| page[‘top’].focus } %\>

JB
