Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7976385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:53:50+00:00 2026-06-04T08:53:50+00:00

I have the following code which is supposed to stop anything from happening when

  • 0

I have the following code which is supposed to stop anything from happening when you click a link except for firing and alert. The link destination is cancelled, but clicking the link scrolls you up to the top of the page. How can I stop this behaviour? Thanks

$('a#target').click(function(event){
    event.preventDefault();
    alert('End of demo');
});

UPDATE my link looks like this:

<a href="#">Submit</a>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T08:53:51+00:00Added an answer on June 4, 2026 at 8:53 am

    No, preventDefault is not supposed to stop anything, it’s supposed to do exactly what it says – prevent the default action. i.e. If you click on a link with an href, it will prevent the default action of the browser which is to go to that link.

    If you want to stop all other events attached to that link, you must return false at the end of the event handler AND make sure that that event handler is executed before any others.

    UPDATE:

    Thanks for posting the link and the href value. I’ll bet that what is happening is that JavaScript encounters an error before that click handler can ever get fired. As the first comment on this answer correctly indicates – preventDefault should prevent the link from jumping to anchors within the page as well. If it’s not, doing it’s job, then it’s probably because there’s a JavaScript error elsewhere on the page.

    All this being said, are you sure you posted the correct HTML?

    $('a#target').click(...
    

    Attached a click handler to a link that looks like this:

    <a href="#" id="target">Submit</a>
    

    And one final thought – if an anchor element only has action(s) attached to it via JavaScript, then why specify the option href attribute at all?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code which is supposed to send a notification on ajax
I have the following code, which is supposed to try to parse a given
I have the following code which is supposed to be removing a particular email
I have the following code, which is supposed to create a form on the
i have the following code, which is supposed to give specific functionality but it
I have the following code which supposed to display a TreeView (the original code
I have the following code which does what it's supposed to do: objSQLCommand =
I have the following code which is supposed to increment a field value by
I have the following code, which is supposed to add a shared_ptr instance to
I have the following code which loads an external script and is supposed to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.