Possible Duplicate:
How do I stop a web page from scrolling to the top when a link is clicked that triggers javascript?
I'm using jquery's slidetoggle to show/hide divs. the element that controls the sliding is a text link ( some text inside <\a>) which has href="#" so it will look like a link (underline, cursor change).
the problem is that when the link is clicked, in addition to the sliding effect, the page scrolls to top.
i tried replacing href="#" with href="" but that disables the div show/hide effect. i guess i could add to the tag Name="somename" and then set the href to href="#somename" but i would rather not use tricks like that.
why is href="#" scrolling the page to its top?
any ideas would be highly appreciated
To make a hyperlink in an HTML page, use the <a> and </a> tags, which are the tags used to define the links. The <a> tag indicates where the hyperlink starts and the </a> tag indicates where it ends. Whatever text gets added inside these tags, will work as a hyperlink. Add the URL for the link in the <a href=” ”>.
For <a> and <area> elements, the href attribute specifies the URL of the page the link goes to. For <base> elements, the href attribute specifies the base URL for all relative URLs on a page. For <link> elements, the href attribute specifies the location (URL) of the external resource (most often a style sheet file).
HTML Link Syntax Links are specified in HTML using the <a> tag. A link or hyperlink could be a word, group of words, or image.
The HTML <a> href Attribute is used to specify the URL of the page that the link goes to. When the href attribute is not present in the <a> an element that it will not be a hyperlink. This attribute is used to specify a link to any address. This attribute is used along with <a> tag.
Several options:
return false;
at the bottom of your click handler and the href wont be followed.javascript:void(0);
Call preventDefault
on the event in your handler:
function( e ) { e.preventDefault(); // your event handling code }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With