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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:56:09+00:00 2026-06-01T01:56:09+00:00

I have run into this problem a few times and I’m not happy with

  • 0

I have run into this problem a few times and I’m not happy with the solutions I’ve used before.

I have an input box with a blur event that validates the content of it and a button which will fill the input box on click. The problem is clicking the button fires the input blur event and then the button click event so content inserted by the button is not what is validated.

See http://jsfiddle.net/jakecr/dL3K3/

I know this is the correct behavior but I can’t think of a clean way to get around the problem.

  • 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-01T01:56:10+00:00Added an answer on June 1, 2026 at 1:56 am

    We had a similar problem at work. what we figured out in the end
    was that the mousedown event will fire before the blur event
    allowing you to set the content before the validation or even cancel the validation
    process completely using a flag.

    check this fiddle I made using your example-
    http://jsfiddle.net/dL3K3/31/

    $(function(){
        var flag = true;
        $('input').blur(function(){
            if(!$(this).val() && flag){
                alert("Grrr, It's empty");
            }
        });
    
        $('button').mousedown(function(){
            flag = false;
        });    
        $('button').mouseup(function(){
            flag = true;
            $('input').val('content').focus();
        });
    
    });
    

    -Edit-

    As @mclin Suggested, using the mousedown event and preventDefault will prevent blur behavior.
    And you can just leave the original click event intact –

    http://jsfiddle.net/dL3K3/364/

    $(function(){
        $('input').blur(function(){
            if(!$(this).val()){
                alert("Grrr, It's empty");
            }
        });
    
        $('button').mousedown(function(e){
            e.preventDefault();
        });    
        $('button').click(function(){
            $('input').val('content');
        });
    
    });
    

    This solution might be a bit cleaner and better for most cases just note that if you use it you’ll preventDefault and blur of any other element the focus is currently on but for most use cases that shouldn’t be an issue.

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

Sidebar

Related Questions

I have run into this problem a few times before so this time around
I have run into this problem a few times and have never come up
I've run into this problem a few times and haven't found any standard solution,
This question is quite simple but I have run into the problem a few
I run into this problem. I have a textarea which I only want to
NOTE: I have solved the majority of this problem but have run into a
I've run into this a few times now and it's getting annoying. In Dojo
I've run into this issue quite a few times and never liked the solution
I seem to run into this situation quite a lot and have yet to
Ok so this is a brand new snag I have run into. I am

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.