Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

href="#" Going to Top of Page - Prevent? [duplicate]

I have a page with some jQuery functions. The HTML on the page looks like so:

<a href="#" class="service">Open</a> 

When I click the Open button a hidden panel slides out. The jQuery itself works great however when I click the button it also takes me to the top of the page.

Is this the defualt behavior and how do I prevent every href="#" from taking me to the top of the page.

Note: I could add id's and tell the href to direct to that ID. I do not want to do that for various reasons (including adding unnecessary code).

like image 517
L84 Avatar asked Oct 21 '12 23:10

L84


People also ask

What is href in HTML?

Definition and Usage The href attribute specifies the URL of the page the link goes to. If the href attribute is not present, the <a> tag will not be a hyperlink. Tip: You can use href="#top" or href="#" to link to the top of the current page!

What is href in URL?

The href attribute link (short for “Hypertext REFerence”) indicates the relationship between pages to search engines. href is an attribute of the anchor tag and contains two components: The URL (the actual link) and. The clickable text or object that users will see on the page (known as the “anchor text”)

What does the a in a href stand for?

href stands for hypertext reference.


2 Answers

<a href="#!" class="service">Open</a> 
like image 73
MyroslavN Avatar answered Sep 22 '22 17:09

MyroslavN


In your event handler, add e.preventDefault(); (assuming e is the name of the variable holding the event):

$('.service').click(function(e) {     e.preventDefault(); }); 
like image 41
icktoofay Avatar answered Sep 23 '22 17:09

icktoofay