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 7931127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:37:56+00:00 2026-06-03T20:37:56+00:00

event.preventDefault(); return false; event.stopPropagation(); any of them can prevent focus event from happening when

  • 0

event.preventDefault();
return false;
event.stopPropagation();

any of them can prevent focus event from happening when I trigger a mousedown event in Firefox and chrome, but it failed in IE.In fact,chrome has another problem. when mousedowning, :hover css is not working.
`

<input type="text" id="name">
<div id="suggest">
  <div>mark</div>
  <div>sam</div>
  <div>john</div>
</div>
$("#name").focus(suggest.show).blur(suggest.hide);

`
what I expected is, when I click the sub div, such as “sam”,and then $(“#name”).val(“sam”).
but the problem is, when I press the mouse(just mousedown,not released), the $(“#name”).blur runs immediately and suggest div becomes hide.

  • 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-03T20:37:56+00:00Added an answer on June 3, 2026 at 8:37 pm

    Use :active instead of :hover for CSS to apply while the mouse button is down.

    I don’t think that preventing the blur event will do quite what you want, since if the user clicks on the input, then on the div, then elsewhere on the page, then the div will remain. I can think of a few different options, each with their own pitfalls. This one will behave correctly while the user remains within the page, but will leave the div showing if the user moves to the address bar:

    $("html").on("focus", "#name", function() {
        $("#suggest").show();
    }).mousedown(function() {
        $("#suggest").hide();
    }).on("mousedown", "#name, #suggest", function(e) {
        e.stopPropagation();
    });​
    

    jsFiddle: http://jsfiddle.net/nbYnt/2/

    If you don’t need to support IE7, then you can do this using just CSS (and it works exactly as expected on any modern browser, including IE8):

    #suggest {
        display: none;
    }
    
    #name:focus + #suggest, #suggest:focus {
        border: none;
        display: block;
    }
    

    Note that you need to put a tabindex on the div for the CSS method to work:

    <div id="suggest" tabindex="-1">
    

    jsFiddle: http://jsfiddle.net/nbYnt/1/

    Finally, you can err on the side of having the div vanish by combining JavaScript with CSS (this works in IE7):

    #suggest:hover {
        display: block !important;
    }
    
    $("#name").focus(function() {
        $("#suggest").show();
    }).blur(function() {
        $("#suggest").hide();
    });
    

    The problem with this method is that after clicking on the div, simply moving the mouse away will cause the div to vanish.

    jsFiddle: http://jsfiddle.net/nbYnt/4/

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

Sidebar

Related Questions

Possible Duplicate: event.preventDefault() vs. return false I'm not sure, but as far as I
I can't get my on('submit') event handler to prevent submitting the form using event.preventDefault();
I'm having issues with event.preventDefault() in firefox, it is not working. This is the
I have tried the following code: $('a.buildMenu').click(function (event) { // Prevent normal behaviour event.preventDefault();
Is there any way to prevent a click from an <a> triggering delegated click
When I want to prevent other event handlers from executing after a certain event
Just curious about which of these would be faster? $('ul.dropdown a').first().click(function(event) { event.stopPropagation(); return
Here is a jsfiddle using POJS showing that return false; doesn't stop the event's
In the example below, return false does not seem to prevent the default action
I'm trying to prevent the default anchor link and onclick event to trigger. Here's

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.