Forum Moderators: open

Message Too Old, No Replies

Div Problem with Show/Hide Javascript

issue with page resetting to top when div displayed and hidden

         

scottonline

3:13 pm on Aug 8, 2008 (gmt 0)

10+ Year Member



Hi everyone,

First post here - just hoping to utilize someone's knowledge with javascript / css.

I am currenty showing/hiding several divs on the same page using javascript and css. Once executed however (onClick), the divs being hidden/shown further down the page cause the page to reset to the very top (not showing the content that was shown/hidden).

Is anyone aware of a way to keep the page at the same place once a link is clicked executing a javascript code?

Any help is greatly appreciated.

Thanks so much,

Scott

Trace

6:03 pm on Aug 8, 2008 (gmt 0)

10+ Year Member



Without knowing how your page is written, my first guess would have to be you're doing something like this

<a href="#" onclick="showHide();">

That will call the function showHide then follow the link, which is #

Change it to this

<a href="#" onclick="showHide(); return false">

Now it calls the function and stops, doesn't follow the link.

If I'm completely wrong, you're going to need to post some exmaple code.

Fotiman

6:09 pm on Aug 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hello and welcome to the Webmasterworld. Does your link look something like this:

<a href="#" onclick="showhidemethod();">...</a>

In the above case, you want to return false to your onclick handler:

<a href="#" onclick="showhidemethod(); return false;">...</a>

That will prevent the link from processing the href link.

A better solution would be to use unobtrusive JavaScript to attach an event listener, and to have that listener stop the event propagation. In other words, you keep your markup clean and free of any scripting and your script will attach listeners to your elements.

[edit]Trace beat me to the punch, and my post is pretty much a duplicate of what Trace said. :-)[/edit]

[edited by: Fotiman at 6:11 pm (utc) on Aug. 8, 2008]

scottonline

8:24 pm on Aug 8, 2008 (gmt 0)

10+ Year Member



Hi Trace/Fotiman,

That's great! Thanks so much for the quick reply. I used A NAME and it seemed to be working alright, but this is a much better solution. In solved my problem with a few lines less code.

Thanks so much - I appreciate it! Have a great weekend.

Scott