I am using anchors and handle the click event in JQuery.
The href is set to “#”
Everything works fine except that the browser scrolls to the top when I click on the anchor. Is there a way to prevent this?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Sure. You need to prevent the default action from occuring by either calling the
event.preventDefault()method or by simply returningfalsefrom your event handler. You don’t need both, but I’ll show both here so you can see how it would fit with your existing code:Note that returning false will also prevent the event from bubbling, i.e., it is the equivalent of calling both
.preventDefault()and.stopPropagation().