views:

257

answers:

2

I have a page of things that looks like this (and yes, I have included the scriptaculous javascripts, because the toggle actually does work):

<html>

<div id="container">
    <div id="thing1" class="thing" >
       <p>Some visible stuff 1</p><a href="#" onclick="Effect.toggle('extra1');">More1</a>
       <div id="extra1" style="display:none;">
         <p>Some extra and initially invisible stuff</p>
       </div>
    </div>

    <div id="thing2" class="thing">
       <p>Some visible stuff 2</p><a href="#" onclick="Effect.toggle('extra2');">More2</a>
       <div id="extra2" style="display:none;">
         <p>Some extra and initially invisible stuff</p>
       </div>
    </div>

   <!-- 96 more entities of class="thing" -->

    <div id="thing99" class="thing">
       <p>Some visible stuff 99</p><a href="#" onclick="Effect.toggle('extra99');">More99</a>
       <div id="extra99" style="display:none;">
         <p>Some extra and initially invisible stuff</p>
       </div>
    </div>
</div>
</html>

When I click on the "More99" link at id="thing99" which is way down at the bottom of the page, , the div at id="extra99" appears as it should, but the page then the page automatically scrolls back to the top.

When I clck the "More1" link at the top, it stays focused at the top.

Is there anyway I can keep the page from scrolling back to the top and/or to refocus it back to where I just clicked? I don't want to scroll back down and find out which one I just made visible.

+2  A: 

Try return false; after your Effect.toggle(); calls.

<a href="#" onclick="Effect.toggle('extra99'); return false;">More99</a>

This isn't the ideal way of handling this, but it should fix your immediate problem. Ideally, you would remove all of your Javascript from your HTML, and do away with all onclick attributes, handling everything from a remote block of Javascript.

Jonathan Sampson
The reason why you need a return false is that otherwise the browser will continue to execute the default action for the link, which is to follow to `#`, which is the same as top of the page.
Tatu Ulmanen
Thanks Jonathan. That got me going on the right track.
Jay Godse
Thanks Tatu. That's exactly what I found out as well.
Jay Godse
Jay, glad I could help. Please consider accepting the answer.
Jonathan Sampson
A: 

Jonathan & Tatu started me on the right track. I tried "return false", and it worked some of the time.

With the "return false" concept in hand, I searched GOogle and stumbled on this page for hrefs and return false.

That "#" tag kept returning me to the top of the page. I tried href="" without the "return false", and it worked as desired, but it randomly reloaded the page as well.

The following solution gave me what I wanted consistently:

<a href="" onclick="Effect.toggle('extra99'); return false;">More99</a>

Everything worked as desired, and no random page reloads.

Jay Godse
Jay, you don't need to have an empty `href` value if you're using `return false`.
Jonathan Sampson
That's what I hypothesized as well. However for some reason, I ended up needing both.
Jay Godse