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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:24:26+00:00 2026-05-26T08:24:26+00:00

Yeah, this is indeed my third or fourth question about jQuery UI Autocomplete. My

  • 0

Yeah, this is indeed my third or fourth question about jQuery UI Autocomplete. My latest annoyance is that clicking the ‘submit’ button in a form does not seem to count as a ‘change’ for jQuery. When my input box changes, jQuery runs some javascript to force a match against a list of acceptable inputs and their values:

jQuery("#Resource").autocomplete({
   source: function( request, response ) {
    var matcher = new RegExp(jQuery.trim(request.term).replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), "i" );
    var matches = jQuery.grep(resources, function(value) {
     return matcher.test( value.label || value.value || value );
    });
    response(matches.length ? matches : [{ label: "No Result Found", value: "" }]);
   },
   focus: function(event, ui) {
    jQuery('#Resource').val(ui.item.label);
    return false;
   },          
   select: function(event, ui) {
    jQuery('#Resource').val(ui.item.label);
    jQuery('#EmployeeNumber').val(ui.item.value);
    return false;
   },
   change: function(event,ui) {
    for(var i in resources){
     jQuery('#EmployeeNumber').val("");
     if(resources[i].label.toLowerCase()==jQuery('#Resource').val().toLowerCase()) {
      jQuery('#EmployeeNumber').val(resources[i].value);
      break;
     }
    }
   }

  });

If the EmployeeNumber (hidden) input already has a value, entering an invalid name and then tabbing or clicking off of the input box clears this value, however if you instead immediately hit the form Submit button, it does not seem to count as a ‘change’ and the POST data includes the previous EmployeeNumber value – making what should be an incorrect input ‘sort of’ correct!

Is there either some way I can make the jQuery recognise that clicking ‘Submit’ is also a change, or else some way I can get the Submit button to notify the jQuery that the Resource input has been clicked off of?

UPDATE:

In the end, I got around this by using what I consider a rather hacky method – I abandoned the ‘change’ event of the auto-complete box and did that matching myself manually when the user clicks submit. The way it work now is that if the user chooses a name by clicking on the auto-complete drop-down menu, the EmployeeNumber value will be filled in by the auto-complete jQuery code. If the user just types a name and hits submit, then the matching is instead done by the forceCheck() function. Of course this function will automatically match against the first name regardless of whether or not there is a second identical name, but unfortunately in what seems like the weirdest quirk I’ve ever seen, any code after the end of that for loop does not get executed, even if there is no ‘return’ statement in the loop. After the loop finishes the submit request just continues… it’s so very odd.

<fieldset class="submit">
    <input id="Submit" name="Submit" type="Submit" class="Submit" value ="Submit" onclick="document.getElementById('Resource').value = jQuery.trim(document.getElementById('Resource').value);forceCheck();"/>
</fieldset>

...


<script type="text/javascript">

    ...

    // Force a resource name check when the user hits the submit button. Necessary because if user changes Resoruce
    // input then hits submit directly (no click or tab off), the jQuery 'change' event (see below) isn't fired
    // until *after* the submit button has begun processing, which is too late.
    function forceCheck() {
        for(var i in resources) {
            document.getElementById("EmployeeNumber").value = "";
            if(resources[i].label.toUpperCase() == document.getElementById("Resource").value.toUpperCase()) {
                document.getElementById("EmployeeNumber").value = resources[i].value;
                return;
            }
        }
    }

    jQuery(function(){
        jQuery("#Resource").autocomplete({
            source: function( request, response ) {
                var matcher = new RegExp(jQuery.trim(request.term).replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), "i" );
                var matches = jQuery.grep(resources, function(value) {
                    return matcher.test( value.label || value.value || value );
                });
                response(matches.length ? matches : [{ label: "No Result Found", value: "" }]);
            },
            focus: function(event, ui) {
                jQuery('#Resource').val(ui.item.label);
                return false;
            },          
            select: function(event, ui) {
                jQuery('#Resource').val(ui.item.label);
                jQuery('#EmployeeNumber').val(ui.item.value);
                return false;
            },  
        });
    }); 
</script>

If anyone knows a better way than this, I would very much appreciate your answer – or if you know why the remainder of my forceCheck function might not run… Otherwise, I will post this post as an answer to my own question tomorrow, and accept it when I can (2 days afterwards, I believe).

Final Update:

Well, it’s been more than 24 hours (quite a lot more), so I’ve posted my solution up as an “answer”. I’ll leave it a few more days then accept it if there’s still no other answer.

  • 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-05-26T08:24:27+00:00Added an answer on May 26, 2026 at 8:24 am

    The reason that the change handler is called to late (after submit) is that autocomplete is coded asynchronously (with a lot of setTimeout(…)).
    The change-event is called 150 ms after the blur-event.

    My solution is also a bit hacky, but you can keep the autocomplete-code apart from the submit. This is also easy to remove if the autocomplete-widget is rewritten in a more synchronous way.

    $("#mysubmit").click(function (event) {
    //Wait for autocomplete to change value
        setTimeout(function() {
            //Do submit
            ...
        }, 250 ); //Make sure delay is greater than 150 ms, as in autocomplete widget.
        event.preventDefault(); //Don't submit now
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello yeah I'm asking this question a second time, sorry about that but I
Before you say, yeah this question can be duplicate; https://stackoverflow.com/questions/2912890/gridview-freeze-pane-solutions How to freeze GridView
Hey so following this Question I've gotten stuck again, and yeah I've tried looking
Yeah, sorry about asking a stupid n00b question. So I have a C# program.
Yeah, this ought to be fun. I'm working on a site that was built
Yeah I've drawn a blank on the this one, and after playing about with
Yeah, I know this seems like a dumb question, its just a one-off hack
Yeah, I know, this question's been asked/answered 34798796873.5 times. I looked through all 3
Yeah this works in FF and Chrome, but for some reason wont work in
Yeah, its a bit on this side of pointless, but I was wondering... I've

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.